Changing existing list using matplotlib event in python
我正在开发一个程序来识别功率密度谱中的重要频率。我已经自动找到了一个重要峰值的列表。但现在我想用fig.canvas.mpl_connect(key_press_event,ontype)从图中直观地观察它们并添加/删除峰值(http://matplotlib.org/users/event_handling.html)
因为我想添加多个峰值,所以我想更新已用列表。尽管我得到了一个未绑定的本地错误:在赋值错误之前引用了本地变量"frequencylist"。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def interactiveMethod(frequency,PDS, frequencyLIST,figureName): #frequency and PDS is the input list of data, frequencyLIST is my found list of #frequencies and figureName is the figure I previously made which I want to use the #event on. def ontype(event): if event.key == 'a': #Getting the event xdata x = event.xdata frequencyCut = frequency[np.where((frequency > x - 2) & (frequency < x + 2))] PDSCut = PDS[np.where((frequency > x - 2) & (frequency < x + 2))] #Find the maximum PDS, as this corresponds to a peak PDSMax = np.max(PDSCut) frequencyMax = frequencyCut[np.where(PDSCut == PDSMax)][0] #Updating the new list using the found frequency frequencyLIST = np.append(frequencyLIST,frequencyMax) figureName.canvas.mpl_connect('key_press_event',ontype) |
我不知道我应该把这个常客放在哪里,这样我就可以更新它了。
python版本:2.7.3 32位
Matplotlib版本:1.3.0
numpy版本:1.7.1
Ubuntu 13.1
我也有完全的树冠(不确定哪种版本)
您正在将
1 2 3 | def ontype(event): global frequencyLIST # ... following lines unaltered ... |
在这个堆栈溢出问题中,您可以了解更多关于python范围变量的方法,以及类似于您的技巧。