title: Getting the location of a mouse click in Matplotlib using Tkinter
body: <p>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.</p>
  
  <p>I found the following thread to get me started; <a href="https://stackoverflow.com/questions/25521120/store-mouse-click-event-coordinates-with-matplotlib">Store mouse click event coordinates with matplotlib</a></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>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()
  </code></pre>