Advertisement
Guest User

pyqt_matplotlib_corr

a guest
Jan 20th, 2012
771
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  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.          self.axes = self.figure_canvas.figure.add_subplot(111)
  30.          x = np.linspace(0.,2*np.pi,100)
  31.          self.axes.plot(x,np.sin(x),label='sin(x) ')
  32.          self.axes.plot(x,np.cos(x),label='cos(x) ')
  33.          self.axes.figure.set_facecolor('white')
  34.          self.axes.grid('on')
  35.          self.axes.legend()
  36.          # add a navigation toolbar
  37.          self.navigation_toolbar = NavigationToolbar2(self.figure_canvas, self)
  38.          layout.addWidget(self.navigation_toolbar, 0)
  39.  
  40.          self.print_button = QPushButton()
  41.          self.print_button.setIcon(QIcon("Fileprint.png"))
  42.          self.print_button.setToolTip("Print the figure")
  43.          self.navigation_toolbar.addWidget(self.print_button)
  44.          self.connect(self.print_button, SIGNAL('clicked()'), self.goPrinter)
  45.  
  46.          self.quit_button = QPushButton("&Quit")
  47.          self.navigation_toolbar.addWidget(self.quit_button)
  48.          self.connect(self.quit_button, SIGNAL('clicked()'), self.close)
  49.          
  50.      def goPrinter(self):
  51.          printer = QPrinter()
  52.          printer.A4
  53.          printer.HighResolution
  54.          printer.Color
  55.          anotherWidget= QPrintDialog(printer,self)
  56.          if(anotherWidget.exec_() != QDialog.Accepted):
  57.              return
  58.          p = QPixmap.grabWidget(self.figure_canvas)
  59.          printLabel = QLabel()
  60.          printLabel.setPixmap(p)
  61.          painter = QPainter(printer)
  62.          printLabel.render(painter)
  63.          painter.end()
  64.  
  65.          show()
  66.  
  67. if __name__=="__main__":
  68.      app=QApplication(sys.argv)
  69.      mw=ViewWidget()
  70.      mw.show()
  71.      sys.exit(app.exec_())
Advertisement
Comments
  • klele
    1 year
    # text 0.10 KB | 0 0
    1. Thank you so much - you showed me the way how to add a QAction into the matplotlib's NavigationToolbar.
Add Comment
Please, Sign In to add comment
Advertisement