2017-07-14 48 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