Matplotlib / python可点击的点 [英] Matplotlib / python clickable points
问题描述
我有一堆时间序列数据,每5秒有一个点数。所以,我可以创建一个线图,甚至可以使数据平滑以获得更平滑的图。问题是,在matplotlib中有没有什么方法或python中的任何方法可以让我点击一个有效的点来做点什么?因此,例如,如果该数据存在于我的原始数据中,我将能够点击(10,75),然后我将能够在Python中执行某些操作。
<任何想法?谢谢。
要扩展@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 pick:',event.artist
print'{} vertices picked'.format(len(ind))
print'在顶点{}和{ }'。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(范围(10),'ro-',选择器=容差)
fig.canvas.callbacks.connect('pick_event',on_pick)
plt.show()
具体如何处理这个问题将取决于您使用的艺术家(换句话说,您是否使用过 ax.plot
与 ax.scatter
与 ax.imshow
?)。
挑选事件根据所选艺术家的不同具有不同的属性。总会有 event.artist
和 event.mouseevent
。大多数具有单独元素的艺术家(例如 Line2D
s, Collections
等)将具有选择为 event.ind
的项目。
如果您想绘制多边形并选择内部点,请参阅: http://matplotlib.org/examples/event_handling/lasso_demo.html#event-handling-example-code-lasso-demo-py
I have a bunch of time series data with points every 5 seconds. So, I can create a line plot and even smooth the data to have a smoother plot. The question is, is there any way in matplotlib or anything in python that will allow me to click on a valid point to do something? So, for example, I would be able to click on (10, 75) if that datum exists in my original data and then I would be able to do something in Python.
Any thoughts? Thanks.
To expand on what @tcaswell said, see the documentation here: http://matplotlib.org/users/event_handling.html
However, you might find a quick demo of pick events useful:
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()
Exactly how you approach this will depend on what artist you're using (In other words, did you use ax.plot
vs. ax.scatter
vs. ax.imshow
?).
Pick events will have different attributes depending on the artist selected. There will always be event.artist
and event.mouseevent
. Most artists that have individual elements (e.g. Line2D
s, Collections
, etc) will have a list of the index of the items selected as event.ind
.
If you'd like to draw a polygon and select points inside, see: http://matplotlib.org/examples/event_handling/lasso_demo.html#event-handling-example-code-lasso-demo-py
这篇关于Matplotlib / python可点击的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!