我正在尝试使用 apick_event
通过单击鼠标直接访问点的确切值:
def plot(self, x_values: list, y_values: list):
def pick_handler(event):
x, y = event.mouseevent.xdata, event.mouseevent.ydata
print(x, y)
...
self.sc, = self.axis.plot(x_values, y_values, 'bo', markersize=7, picker=7)
self.fig.canvas.mpl_connect('pick_event', pick_handler)
...
Run Code Online (Sandbox Code Playgroud)
问题是我没有得到确切的值,因为picker
设置为 7。有没有办法在不计算最近点的情况下获取这些值?
谢谢!
当然,您不想知道鼠标的位置 ( event.mouseevent.xdata
),而是想知道事件的索引 ( ),以便从所选event.ind
艺术家 ( ) 中选择正确的值。event.artist
您所要求的是matplotlib 事件指南 中此示例的一部分。
我只能引用一下:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on points')
line, = ax.plot(np.random.rand(100), 'o', picker=5) # 5 points tolerance
def onpick(event):
thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
ind = event.ind
points = tuple(zip(xdata[ind], ydata[ind]))
print('onpick points:', points)
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4283 次 |
最近记录: |