title: Highlighting corresponding points in adjacent plots
body: <p>I have a problem with highlighting corresponding points on plots in python using matplotlib. I tried to do it by using a function that reads data from plots when I click the point with a mouse(onpick). Then I try to annotate this information in other adjacent plots. I have a problem with using variables from the onpick function in the annotate command. I would like to use the <code>ind</code> parameter from the <code>onpick</code> function in the <code>annotate</code> command, but python doesn't see it. This is the <code>onpick</code> function:</p>
  
  <pre><code>def onpick(event):
      thisline = event.artist
      xdata = thisline.get_xdata()
      ydata = thisline.get_ydata()
      global ind
      ind = event.ind
      print('x:', xdata[ind],'y:',ydata[ind])
  </code></pre>
  
  <p>I call the function using this command:</p>
  
  <pre><code>fig.canvas.mpl_connect('pick_event', onpick)
  </code></pre>
  
  <p>Annotating:</p>
  
  <pre><code>ax = plt.subplot2grid((lg, 2), (0, 0), colspan=2)
      ax.plot(t, mp, picker=1)
      ax.set_title('map')
      ax.annotate('here',(t[ind],mp[ind]),xytext=(0.8,0.9),
                  arrowprops=dict(facecolor='grey',color='grey'))
  </code></pre>
  
  <p>Python command box shows this:</p>
  
  <pre class="lang-none prettyprint-override"><code>Exception in Tkinter callback
  Traceback (most recent call last):
    File "C:\Users\kubag\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
      return self.func(*args)
    File "C:/Users/kubag/Desktop/pyton/wykres/wykres.py", line 78, in graph
      ax.annotate('here',(t[ind],mp[ind]),xytext=(0.8,0.9),arrowprops=dict(facecolor='grey',color='grey'))
  NameError: name 'ind' is not defined
  </code></pre>
  
  <p>Whole code:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  t=range(10)
  mp=range(10)
  v=range(10)
  
  def onpick(event):
      thisline = event.artist
      xdata = thisline.get_xdata()
      ydata = thisline.get_ydata()
      global ind
      ind = event.ind
      print('x:',int(xdata[ind]),'y:',int(ydata[ind]))
      global ind2
      ind2=int(ind)
      ax.annotate('miejsce', (t[ind2], mp[ind2]), xytext=(0.8, 0.9), arrowprops=dict(facecolor='grey'))
  
  fig = plt.figure()
  global ax
  ax = plt.subplot2grid((1, 2), (0, 0))
  ax.plot(t, mp, picker=1)
  
  ax1 = plt.subplot2grid((1, 2), (0, 1))
  ax1.plot(t, v, picker=1)
  
  fig.canvas.mpl_connect('pick_event', onpick)
  fig.canvas.draw()
  plt.tight_layout()
  plt.show()
  </code></pre>