如何在交互式绘图(Python)中用鼠标指向(x,y)位置? [英] How to get a (x,y) position pointing with mouse in a interactive plot (Python)?

查看:34
本文介绍了如何在交互式绘图(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)位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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