获取 matplotlib 图中显示的线条限制?

发布于 2025-01-13 03:41:51 字数 976 浏览 1 评论 0原文

假设我有这个简单的 matplotlib 图:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x_data = [0, 1.0]
y_data = [0, 1.0]
myline = ax.plot(x_data, y_data)

def on_keypress(event):
  if event.key == 'ctrl+p':
    print("Printing myline: {}".format(myline))

fig.canvas.mpl_connect('key_press_event', on_keypress)

plt.show()

假设我运行这个,然后我做了一个“缩放到矩形”:

plot image full

...因此,我显示了这个缩放区域:

缩放绘图图像

如何找到所示线段的端点坐标?

在示例代码中,当我按 CTRL+p 时,会打印一行 - 当我按下该键时,我希望显示 的坐标 要打印的线段,无论显示哪个线段(如果显示整条线,则 (0,0)、(1,1) 将是预期打印的端点)。

我怎样才能做到这一点?当然,我希望该方法适用于任意行,而不仅仅是 (0,0), (1,1) 行......

Say I have this simple matplotlib plot:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x_data = [0, 1.0]
y_data = [0, 1.0]
myline = ax.plot(x_data, y_data)

def on_keypress(event):
  if event.key == 'ctrl+p':
    print("Printing myline: {}".format(myline))

fig.canvas.mpl_connect('key_press_event', on_keypress)

plt.show()

Say, I run this, and I make a "Zoom to rectangle":

plot image full

... and thus, I get this zoomed region shown:

plot image zoomed

How can I find the coordinates of the endpoints of the shown segment of the line?

In the example code, a line is printed when I press CTRL+p - and when I hit that, I'd like the coordinates of the shown segment of the line to be printed, regardless of which segment is shown (if whole line is shown, then (0,0), (1,1) would be expected endpoints printed).

How can I do that? Of course, I'd like the method to work for an arbitrary line, not just the (0,0), (1,1) one ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥繁 2025-01-20 03:41:51

这是使用 shapely 的方法:

from shapely.geometry import LineString, box
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x_data = [0.7, 1.0]
y_data = [2.1, 3.4]
myline, = ax.plot(x_data, y_data)

def on_keypress(event):
  if event.key == 'ctrl+p':
    viewlim = ax.viewLim   
    linestr = LineString(myline.get_xydata())
    viewbox = box(viewlim.x0, viewlim.y0, viewlim.x1, viewlim.y1)    
    inters = viewbox.intersection(linestr)
    coords = np.asarray(inters.coords)
    print(coords)

fig.canvas.mpl_connect('key_press_event', on_keypress)

plt.show()

示例输出:
输入图片这里的描述

>>>[[0.79071429 2.49309524]
>>>[1.         3.4       ]]

Matplotlib 内部必须有类似的东西来计算 clip on 参数;但是,我没有立即在源代码中找到它。

Here is an approach using shapely:

from shapely.geometry import LineString, box
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x_data = [0.7, 1.0]
y_data = [2.1, 3.4]
myline, = ax.plot(x_data, y_data)

def on_keypress(event):
  if event.key == 'ctrl+p':
    viewlim = ax.viewLim   
    linestr = LineString(myline.get_xydata())
    viewbox = box(viewlim.x0, viewlim.y0, viewlim.x1, viewlim.y1)    
    inters = viewbox.intersection(linestr)
    coords = np.asarray(inters.coords)
    print(coords)

fig.canvas.mpl_connect('key_press_event', on_keypress)

plt.show()

Sample output:
enter image description here

>>>[[0.79071429 2.49309524]
>>>[1.         3.4       ]]

Matplotlib must have something similar internally to calculate clip on parameters; however, I did not immediately find it in the source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文