I am trying to write a simple GUI in python that displays a plot and then allows the user to click on certain key characteristics (turning points etc), which will then be used as a starting point for a fitting algorithm I'm developing.
I found the following thread to get me started; Store mouse click event coordinates with matplotlib
This only seems to give me the pixel locations of the mouse from my computer's perspective. What I would ideally like is to be able to get at the coordinates on the right hand side of the toolbar and use those; it seems a shame to try to write my own pixel->data transform when matplotlib is clearly already doing it for me somewhere.
Here is the code I have so far, if it looks like I'm approaching the problem in the wrong manner please tell me, I'm not too proud to start over.
import matplotlib
matplotlib.use('TkAgg')
import Tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import MouseEvent
from matplotlib.figure import Figure
import numpy as np
def callback(event):
print "clicked at", event.x, event.y
root = tk.Tk()
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = np.arange(0.0,3.0,0.01)
s = np.sin(2*np.pi*t)
a.plot(t,s)
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.mpl_connect('button_press_event',callback)
def callback(event):
print "clicked at", event.xdata, event.ydata
toolbar = NavigationToolbar2TkAgg( canvas, root )
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar
root.mainloop()