gpt4 book ai didi

python - Matplotlib/python 可点击点

转载 作者:太空狗 更新时间:2023-10-30 00:36:12 24 4
gpt4 key购买 nike

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

有什么想法吗?谢谢。

最佳答案

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

但是,您可能会发现 pick 事件的快速演示很有用:

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。大多数具有独立元素(例如 Line2DCollections 等)的艺术家都会将所选项目的索引列表作为 event.ind

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

关于python - Matplotlib/python 可点击点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22052532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com