【发布时间】:2017-07-14 19:24:17
【问题描述】:
我正在尝试在类中插入此 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()
但是当鼠标点击一个点时我得到这个错误。
AttributeError: 'MouseEvent' object has no attribute 'artist'
这可能是什么原因? 当不在类内时,代码可以完美运行
非常感谢
【问题讨论】:
标签: python events matplotlib picker