How to get a (x,y) position pointing with mouse in a interactive plot (Python)?(如何在交互式绘图(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.
这篇关于如何在交互式绘图(Python)中用鼠标指向(x,y)位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在交互式绘图(Python)中用鼠标指向(x,y)位置?


基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01