title: Cropping image with PyQt and matplotlib RectangleSelector
body: <p>I am currently designing an application with matplotlib and PyQT. The UI contains several buttons and matplotlib widgets.</p>
  
  <p>UI design is dynamic linked by using uic function as this 
  <a href="https://www.safaribooksonline.com/blog/2014/01/22/create-basic-gui-using-pyqt/" rel="nofollow noreferrer">link</a>.</p>
  
  <p>One button function is to interactively crop the image. I have tried to use <a href="http://matplotlib.org/examples/widgets/rectangle_selector.html" rel="nofollow noreferrer">RectangleSelector</a> of matplotlib, but it doesn't work. I also tried <a href="https://stackoverflow.com/questions/9048350/imrect-for-python">this</a> example.</p>
  
  <p>My code:</p>
  
  <pre><code>import sys
  from PyQt4 import QtCore, QtGui, uic
  from os.path import expanduser
  import matplotlib.pyplot as plt
  import matplotlib.image as mpimg
  from matplotlib.backends import qt4_compat
  from matplotlib.figure import Figure
  from matplotlib.widgets import RectangleSelector
  
  form_class = uic.loadUiType("MTF_color_test.ui")[0]
  
  class Ui_MainWindow(QtGui.QMainWindow, form_class):
      def __init__(self, parent=None):
          QtGui.QMainWindow.__init__(self, parent)
          self.setupUi(self)
          self.pushButtonLoad.clicked.connect(self.LoadImage)
          self.pushButtonCrop.clicked.connect(self.CropImage)
          self.actionLoad.triggered.connect(self.LoadImage)
  
      def LoadImage(self):
          Path = str(QtGui.QFileDialog.getOpenFileName(None,
                                                            "Open an image",
                                                            expanduser("~"),
                                                             "Images (*.jpg *.png)"))
          if Path != '' and (Path.endswith(".jpg") or Path.endswith(".png")):
              print("Image file loaded")
              image = mpimg.imread(Path)
              self.widgetImage.canvas.ax.clear()
              self.widgetImage.canvas.ax.imshow(image)
              self.widgetImage.canvas.draw()
  
      def onselect(self, eclick, erelease):
          if eclick.ydata&gt;erelease.ydata:
              eclick.ydata,erelease.ydata=erelease.ydata,eclick.ydata
          if eclick.xdata&gt;erelease.xdata:
              eclick.xdata,erelease.xdata=erelease.xdata,eclick.xdata
          ax.set_ylim(erelease.ydata,eclick.ydata)
          ax.set_xlim(eclick.xdata,erelease.xdata)
          self.canvas.draw()
          print ' startposition : (%f, %f)' % (eclick.xdata, eclick.ydata)
          print ' endposition   : (%f, %f)' % (erelease.xdata, erelease.ydata)
          print ' used button   : ', eclick.button        
  
      def CropImage(self):
          canvas = self.widgetImage.canvas
          ax = canvas.ax
          print canvas
          print ax
          rs=RectangleSelector(ax, self.onselect, drawtype='box',
                                       rectprops = dict(facecolor='red', edgecolor = 'black', alpha=0.5, fill=True))
          print(rs)
          canvas.draw()
  
  
  app = QtGui.QApplication(sys.argv)
  Ui_MainWindow = Ui_MainWindow(None)
  Ui_MainWindow.show()
  app.exec_()
  </code></pre>