2014-02-26 106 views
3

我有一堆時間系列數據,每隔5秒點數。所以,我可以創建一個線圖,甚至可以使數據平滑以獲得更平滑的圖。問題是,在matplotlib中有沒有什麼方法或python中的任何方法可以讓我點擊一個有效的點來做點什麼?因此,例如,如果該數據存在於我的原始數據中,我將能夠點擊(10,75),然後我將能夠在Python中執行某些操作。Matplotlib/python clickable points

有什麼想法?謝謝。

+2

是,看到選擇器演示 – tacaswell

回答

6

要展開什麼@tcaswell說,在這裏看到的文檔:http://matplotlib.org/users/event_handling.html

然而,你可能會發現有用選秀事件的快速演示:

import matplotlib.pyplot as plt 

def on_pick(event): 
    artist = event.artist 
    xmouse, ymouse = event.mouseevent.xdata, event.mouseevent.ydata 
    x, y = artist.get_xdata(), artist.get_ydata() 
    ind = event.ind 
    print 'Artist picked:', event.artist 
    print '{} vertices picked'.format(len(ind)) 
    print 'Pick between vertices {} and {}'.format(min(ind), max(ind)+1) 
    print 'x, y of mouse: {:.2f},{:.2f}'.format(xmouse, ymouse) 
    print 'Data point:', x[ind[0]], y[ind[0]] 
    print 

fig, ax = plt.subplots() 

tolerance = 10 # points 
ax.plot(range(10), 'ro-', picker=tolerance) 

fig.canvas.callbacks.connect('pick_event', on_pick) 

plt.show() 

正是你如何處理這將取決於你正在使用什麼藝術家(換句話說,你使用ax.plot而不是ax.scatter而不是ax.imshow?)。

根據所選藝術家的不同,挑選事件將具有不同的屬性。總會有event.artistevent.mouseevent。大多數具有單獨元素的藝術家(例如Line2D s,Collections等)將具有選擇爲event.ind的項目的索引列表。

如果你想繪製多邊形,然後選擇內部的點,請參閱:http://matplotlib.org/examples/event_handling/lasso_demo.html#event-handling-example-code-lasso-demo-py