title: Matplotlib: responding to click events
body: <p>I want to do the following, using matplotlib:</p>
  
  <ol>
  <li><p>Create a line between two points, by doing the following:
  i. Double click on canvas using Left button (first point created)
  ii. Either drag mouse to (or simply click on) second point
  ii. Have line drawn between first and second point</p></li>
  <li><p>Place a green (or any other color) circle on the canvas, by doing the following:
  i. Double clicking on canvas, using RIGHT button</p></li>
  <li><p>Since I'm likely to make mistakes when double clicking, I want to be able to select a plotted circle (or line), and press the delete button to delete the selected item.</p></li>
  </ol>
  
  <p>Back in the good old days of VB, this was a 15 minute job. After wasting several hours on this, I have run out of ideas.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  
  class LineDrawer(object):
      lines = []
      def draw_line(self):
          ax = plt.gca()
          xy = plt.ginput(2)
  
          x = [p[0] for p in xy]
          y = [p[1] for p in xy]
          line = plt.plot(x,y)
          ax.figure.canvas.draw()
  
          self.lines.append(line)
  
  
  def onclick(event):
      if event.dblclick:
          if event.button == 1:
              # Draw line
              ld = LineDrawer()
              ld.draw_line() # here you click on the plot
          elif event.button == 3:
              # Write to figure
              plt.figtext(3, 8, 'boxed italics text in data coords', style='italic', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
              circ = plt.Circle((event.x, event.y), radius=0.07, color='g')
              ax.add_patch(circ)
              plt.draw()
          else:
              pass # Do nothing
  
  
  def onpick(event):
      thisline = event.artist
      xdata = thisline.get_xdata()
      ydata = thisline.get_ydata()
      ind = event.ind
      print ('onpick points:', zip(xdata[ind], ydata[ind]))
  
  
  
  fig, ax = plt.subplots()
  
  connection_id = fig.canvas.mpl_connect('button_press_event', onclick)
  fig.canvas.mpl_connect('pick_event', onpick)
  
  
  plt.tight_layout()
  
  plt.show()
  </code></pre>
  
  <p>Apart from the delete functionality, which I haven't even got round to yet, why is my code not performing requirements 1 and 2?</p>
  
  <p>What am I doing wrong?, more importantly, how do I fix the code to get the required functionality?</p>