Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Obs. Something similar to CURSOR Procedure in IDL

like image 714
nandhos Avatar asked Mar 31 '15 22:03

nandhos


1 Answers

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.

like image 102
Hagne Avatar answered Oct 28 '22 16:10

Hagne