2017-07-14 40 views
0

我试图插入这条巨蟒文件中所示的事件选择器例如,一个类里面Matplotlib事件选择器 - 里面一类

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' 

这可能是什么原因? 当不是类中的代码工作完美

感谢很多

回答

-2

我怀疑代码工作类之外。您在这里遇到的问题是您使用的'button_press_event',它没有artist属性。无论是在课堂还是在课堂之外,这都不会改变。

+0

谢谢@ImportanceOfBeingErnest。奇迹般有效。不能相信我花了整整一天的时间。 – ABCD