Matplotlib/python 可点击点 [英] Matplotlib / python clickable points

查看:30
本文介绍了Matplotlib/python 可点击点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆时间序列数据,每 5 秒有一个点.所以,我可以创建一个线图,甚至可以平滑数据以获得更平滑的图.问题是,在 matplotlib 或 python 中有什么方法可以让我点击一个有效的点来做某事吗?因此,例如,如果该数据存在于我的原始数据中,我将能够单击 (10, 75),然后我将能够在 Python 中执行某些操作.

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.

有什么想法吗?谢谢.

推荐答案

要扩展 @tcaswell 所说的内容,请参阅此处的文档:http://matplotlib.org/users/event_handling.html

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()

具体的处理方式取决于您使用的艺术家(换句话说,您使用的是 ax.plot 还是 ax.scatter 还是 <代码>ax.imshow?).

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 事件将具有不同的属性,具体取决于所选的艺术家.总会有event.artistevent.mouseevent.大多数具有单个元素(例如 Line2Ds、Collections 等)的艺术家都会将所选项目的索引列表作为 event.ind.

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. Line2Ds, Collections, etc) will have a list of the index of the items selected as event.ind.

如果您想绘制多边形并选择其中的点,请参阅:http://matplotlib.org/examples/event_handling/lasso_demo.html#event-handling-example-code-lasso-demo-py

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆