获取 matplotlib 图中显示的线条限制?
假设我有这个简单的 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()
假设我运行这个,然后我做了一个“缩放到矩形”:
...因此,我显示了这个缩放区域:
如何找到所示线段的端点坐标?
在示例代码中,当我按 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":
... and thus, I get this zoomed region shown:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用 shapely 的方法:
示例输出:
Matplotlib 内部必须有类似的东西来计算
clip on
参数;但是,我没有立即在源代码中找到它。Here is an approach using shapely:
Sample output:
Matplotlib must have something similar internally to calculate
clip on
parameters; however, I did not immediately find it in the source code.