如何在交互式绘图(Python)中用鼠标指向(x,y)位置?
问题描述
我使用 ipython 笔记本(带有神奇的 %matplotlib nbagg
).我正在查看 matplotlib.widget.Cursor
但仅查看光标 widgets.Cursor.所以我想在图中选择两个点并获取初始和最终的 x,y 位置(例如时间与温度,选择点必须返回初始和最终时间).我需要它来手动选择任意间隔.我认为它类似于 获取全局 x,y 位置,但我在那篇文章中不太了解.
I use ipython notebook (with the magic %matplotlib nbagg
). I was reviewing the matplotlib.widget.Cursor
but the cursor is only viewed widgets.Cursor. So I'd like to select two points clicking in the plot and get the initial and final x,y-position (e.g. time vs Temperature, selecting to points must return initial and final time). I need it for selecting manually an arbitrary interval. I think it's similar to get global x,y position, but I didn't understand well in that post.
如何在交互式绘图 (Python) 中获得鼠标指向的 (x,y) 位置?
How can I get a (x,y) position pointing with mouse in a interactive plot (Python)?
观察.类似于 IDL 中的 CURSOR 过程
Obs. Something similar to CURSOR Procedure in IDL
解决方案
事件处理应该工作.
import matplotlib.pylab as plt
import numpy as np
f,a = plt.subplots()
x = np.linspace(1,10,100)
y = np.sin(x)
a.plot(x,y)
pos = []
def onclick(event):
pos.append([event.xdata,event.ydata])
f.canvas.mpl_connect('button_press_event', onclick)
f.show()
每次单击时,当前光标位置都会附加到 pos.
On each click the current cursor position is appended to pos.
相关文章