类内的Matplotlib事件选择器

2024-10-02 00:25:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在类中插入这个python文档中显示的事件选择器示例

http://matplotlib.org/users/event_handling.html

代码是这样的

import numpy as np
import matplotlib.pyplot as plt


class Test:
    def __init__(self,line):
        self.line = line
        self.cidpress = self.line.figure.canvas.mpl_connect('button_press_event', self.onpick)

    def onpick(self, 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 = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on points')

line, = ax.plot(np.random.rand(10), 'o', picker=5)  # 5 points tolerance
a = Test(line)

plt.show()

但我得到这个错误时,鼠标点击一个点。在

^{pr2}$

这可能是什么原因? 当不在类中时,代码可以完美地工作

多谢了


Tags: 代码importselfeventmatplotlibaslineplt
1条回答
网友
1楼 · 发布于 2024-10-02 00:25:25

我怀疑这些代码在类之外是否有效。您在这里面临的问题是使用'button_press_event',它没有artist属性。无论是在课堂内外,这都不会改变。在

相关问题 更多 >

    热门问题