如何在交互式绘图(Python)中用鼠标指向(x,y)位置?

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)