title: Update plot on mouse click, after pressing a button and plotting, Python
body: <p><strong>Hello!</strong>
  So I got this button Im clicking to plot a figure then I want that figure to update with every mouse buttton push to draw out a cross hair.</p>
  <p>But I can't get it to update the figure after the pressing the button but it works great if I just plot outside on the class.</p>
  <pre><code>import cross_hair
  import numpy as np
  import matplotlib.pyplot as plt
  
  
  from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
  </code></pre>
  <p>import sys</p>
  <pre><code>class PushButton(QWidget):
      def __init__(self):
          super(PushButton,self).__init__()
          self.initUI()
  
      def initUI(self):
          self.setWindowTitle(&quot;PushButton&quot;)
          self.setGeometry(400,400,300,260)
          self.closeButton = QPushButton(self)
          self.closeButton.setText(&quot;Press&quot;)          #text                     
          self.closeButton.clicked.connect(self.button_pressed)
  
  
      def button_pressed(self):
          #Fuction variables
  
          self.x= np.arange(0, 1, 0.01)
          self.y = np.sin(2 * 2 * np.pi * self.x)
          #Figure
          self.fig, self.ax = plt.subplots()
          #title
          self.ax.set_title('Snapping cursor')
          #Plotting
          self.line, = self.ax.plot(self.x, self.y, 'o')
          #
          snap_cursor = cross_hair.SnappingCursor(self.ax, self.line)
          self.fig.canvas.mpl_connect('button_press_event', snap_cursor.on_mouse_click)
          plt.show()
  
  
  if __name__ == '__main__':
      app = QApplication(sys.argv)
      ex = PushButton()
      ex.show()
      sys.exit(app.exec_())
  </code></pre>
  <p><strong>cross hair code looks like this (cross_hair.py)</strong></p>
  <pre><code>import numpy as np
  </code></pre>
  <p>class SnappingCursor:</p>
  <pre><code>def __init__(self, ax, line):
      self.ax = ax
      self.horizontal_line = ax.axhline(color='k', lw=0.8, ls='--')
      self.vertical_line = ax.axvline(color='k', lw=0.8, ls='--')
      self.x, self.y = line.get_data()
      self._last_index = None
      # text location in axes coords
      self.text = ax.text(0.72, 0.9, '', transform=ax.transAxes)
  
  def set_cross_hair_visible(self, visible):
      need_redraw = self.horizontal_line.get_visible() != visible
      self.horizontal_line.set_visible(visible)
      self.vertical_line.set_visible(visible)
      self.text.set_visible(visible)
      return need_redraw
  
  def on_mouse_click(self, event):
      if not event.inaxes:
          self._last_index = None
          need_redraw = self.set_cross_hair_visible(False)
          print('you no pressed' )#, event.button, event.xdata, event.ydata)
  
          if need_redraw:
              self.ax.figure.canvas.draw()
      else:
          self.set_cross_hair_visible(True)
          x, y = event.xdata, event.ydata
  
          print('you pressed')  # , event.button, event.xdata, event.ydata)
  
          index = min(np.searchsorted(self.x, x), len(self.x) - 1)
          if index == self._last_index:
              return  # still on the same data point. Nothing to do.
          self._last_index = index
          x = self.x[index]
          y = self.y[index]
          # update the line positions
          self.horizontal_line.set_ydata(y)
          self.vertical_line.set_xdata(x)
          self.text.set_text('x=%1.2f, y=%1.2f' % (x, y))
  
          self.ax.figure.canvas.draw()
  </code></pre>