简体   繁体   中英

How get a (x,y) position pointing with mouse in a interactive plot (Python)?

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 (eg 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. Something similar to CURSOR Procedure in IDL

Event handling should work.

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()

On each click the current cursor position is appended to pos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM