Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拾取事件以使用matplotlib获取点_Python_Matplotlib - Fatal编程技术网

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