This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Pyqt_event_handling_annotation

By: a guest on Feb 29th, 2012  |  syntax: Python  |  size: 3.19 KB  |  views: 116  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import sys
  2. import numpy as np
  3. from PyQt4.QtCore import *
  4. from PyQt4.QtGui import *
  5. #from xlwt import *
  6. #from pylab import plot, show
  7.  
  8. from matplotlib.figure import Figure
  9. from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  10. from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar2
  11.  
  12.  
  13. class ViewWidget(QMainWindow):
  14.      def __init__(self):
  15.          QMainWindow.__init__(self)
  16.  
  17.          # create a simple main widget to keep the figure
  18.          self.mainWidget = QWidget()
  19.          self.setCentralWidget(self.mainWidget)
  20.  
  21.          layout = QVBoxLayout()
  22.          self.mainWidget.setLayout(layout)
  23.  
  24.          # create a figure
  25.          self.figure_canvas = FigureCanvas(Figure())
  26.          layout.addWidget(self.figure_canvas, 10)
  27.  
  28.          # and the axes for the figure
  29.  
  30.          self.axes = self.figure_canvas.figure.add_subplot(111)
  31.          
  32.          # Bind the 'pick' event for clicking on one of the curves
  33.  
  34.          cid = self.figure_canvas.mpl_connect('pick_event',self.onpick)
  35.          
  36.          x = np.linspace(0.,2*np.pi,100)
  37.          self.axes.plot(x,np.sin(x),label='sin(x) ',picker=5)
  38.          self.axes.plot(x,np.cos(x),label='cos(x) ',picker=5)
  39.          self.axes.figure.set_facecolor('white')
  40.          self.axes.grid('on')
  41.          self.axes.legend()
  42.          self.figure_canvas.draw()
  43.  
  44.          # add a navigation toolbar
  45.  
  46.          self.navigation_toolbar = NavigationToolbar2(self.figure_canvas, self)
  47.          layout.addWidget(self.navigation_toolbar, 0)
  48.  
  49.          self.print_button = QPushButton()
  50.          self.print_button.setIcon(QIcon("Fileprint.png"))
  51.          self.print_button.setToolTip("Print the figure")
  52.          self.navigation_toolbar.addWidget(self.print_button)
  53.          self.connect(self.print_button, SIGNAL('clicked()'), self.goPrinter)
  54.  
  55.          self.quit_button = QPushButton("&Quit")
  56.          self.navigation_toolbar.addWidget(self.quit_button)
  57.          self.connect(self.quit_button, SIGNAL('clicked()'), self.close)
  58.  
  59.      def onpick(self, event):
  60.          thisline = event.artist
  61.          xdata = thisline.get_xdata()
  62.          ydata = thisline.get_ydata()        
  63.          ind = event.ind
  64.          
  65.          print str(xdata[ind][0])
  66.          print ind[0], ydata[ind][0]          
  67.          print xdata[ind][0],  ydata[ind][0]
  68. #
  69. #        The annotation doesn't work. For what reason ?
  70. #
  71.          self.axes.annotate(str(xdata[ind][0]), xy=(ind[0], ydata[ind][0]), xycoords='data', xytext=(xdata[ind][0], ydata[ind][0]))
  72.          self.figure_canvas.draw()
  73.        
  74.      def goPrinter(self):
  75.          printer = QPrinter()
  76.          printer.A4
  77.          printer.HighResolution
  78.          printer.Color
  79.          anotherWidget= QPrintDialog(printer,self)
  80.          if(anotherWidget.exec_() != QDialog.Accepted):
  81.              return
  82.          p = QPixmap.grabWidget(self.figure_canvas)
  83.          printLabel = QLabel()
  84.          printLabel.setPixmap(p)
  85.          painter = QPainter(printer)
  86.          printLabel.render(painter)
  87.          painter.end()
  88.  
  89. #         self.show()
  90.  
  91. if __name__=="__main__":
  92.      app=QApplication(sys.argv)
  93.      mw=ViewWidget()
  94.      mw.show()
  95.      sys.exit(app.exec_())
clone this paste RAW Paste Data