Python 拾取事件以使用matplotlib获取点
我正在尝试使用Python 拾取事件以使用matplotlib获取点,python,matplotlib,Python,Matplotlib,我正在尝试使用拾取_事件通过鼠标单击直接访问点的精确值: 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
拾取_事件
通过鼠标单击直接访问点的精确值:
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)
...
问题是我没有得到确切的值,因为选择器设置为7。
有没有一种不用计算最近点就能得到这些值的方法
谢谢 当然,您不想知道鼠标的位置(event.mouseevent.xdata
),而是想知道事件的索引(event.ind
),以便从拾取的艺术家(event.artist
)中选择正确的值
你所要求的是你生活的一部分
我只能引述:
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()
这是正确的!谢谢,尽管我不得不使用ind=event.ind[0]
而不是ind=event.ind