如何在交互式绘图(Python)中获得用鼠标指向的(x,y)位置? [英] How get a (x,y) position pointing with mouse in a interactive plot (Python)?
问题描述
我使用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.
Obs。类似于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.
这篇关于如何在交互式绘图(Python)中获得用鼠标指向的(x,y)位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!