1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | # embedding_in_qt4.py --- Simple Qt4 application embedding matplotlib canvases |
---|
4 | # |
---|
5 | # Copyright (C) 2005 Florent Rougon |
---|
6 | # 2006 Darren Dale |
---|
7 | # |
---|
8 | # This file is an example program for matplotlib. It may be used and |
---|
9 | # modified with no restriction; raw copies as well as modified versions |
---|
10 | # may be distributed without limitation. |
---|
11 | |
---|
12 | from __future__ import unicode_literals |
---|
13 | import sys, os, random |
---|
14 | from PySide import QtGui, QtCore |
---|
15 | |
---|
16 | import matplotlib |
---|
17 | matplotlib.rcParams['backend.qt4']='PySide' |
---|
18 | |
---|
19 | from numpy import arange, sin, pi |
---|
20 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
21 | from matplotlib.figure import Figure |
---|
22 | |
---|
23 | progname = os.path.basename(sys.argv[0]) |
---|
24 | progversion = "0.1" |
---|
25 | |
---|
26 | |
---|
27 | class MyMplCanvas(FigureCanvas): |
---|
28 | """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.).""" |
---|
29 | def __init__(self, parent=None, width=5, height=4, dpi=100): |
---|
30 | fig = Figure(figsize=(width, height), dpi=dpi) |
---|
31 | self.axes = fig.add_subplot(111) |
---|
32 | # We want the axes cleared every time plot() is called |
---|
33 | self.axes.hold(False) |
---|
34 | |
---|
35 | self.compute_initial_figure() |
---|
36 | |
---|
37 | # |
---|
38 | FigureCanvas.__init__(self, fig) |
---|
39 | self.setParent(parent) |
---|
40 | |
---|
41 | FigureCanvas.setSizePolicy(self, |
---|
42 | QtGui.QSizePolicy.Expanding, |
---|
43 | QtGui.QSizePolicy.Expanding) |
---|
44 | FigureCanvas.updateGeometry(self) |
---|
45 | |
---|
46 | def compute_initial_figure(self): |
---|
47 | pass |
---|
48 | |
---|
49 | |
---|
50 | class MyStaticMplCanvas(MyMplCanvas): |
---|
51 | """Simple canvas with a sine plot.""" |
---|
52 | def compute_initial_figure(self): |
---|
53 | t = arange(0.0, 3.0, 0.01) |
---|
54 | s = sin(2*pi*t) |
---|
55 | self.axes.plot(t, s) |
---|
56 | |
---|
57 | |
---|
58 | class MyDynamicMplCanvas(MyMplCanvas): |
---|
59 | """A canvas that updates itself every second with a new plot.""" |
---|
60 | def __init__(self, *args, **kwargs): |
---|
61 | MyMplCanvas.__init__(self, *args, **kwargs) |
---|
62 | timer = QtCore.QTimer(self) |
---|
63 | QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self.update_figure) |
---|
64 | timer.start(1000) |
---|
65 | |
---|
66 | def compute_initial_figure(self): |
---|
67 | self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r') |
---|
68 | |
---|
69 | def update_figure(self): |
---|
70 | # Build a list of 4 random integers between 0 and 10 (both inclusive) |
---|
71 | l = [ random.randint(0, 10) for i in range(4) ] |
---|
72 | |
---|
73 | self.axes.plot([0, 1, 2, 3], l, 'r') |
---|
74 | self.draw() |
---|
75 | |
---|
76 | |
---|
77 | class ApplicationWindow(QtGui.QMainWindow): |
---|
78 | def __init__(self): |
---|
79 | QtGui.QMainWindow.__init__(self) |
---|
80 | self.setAttribute(QtCore.Qt.WA_DeleteOnClose) |
---|
81 | self.setWindowTitle("application main window") |
---|
82 | |
---|
83 | self.file_menu = QtGui.QMenu('&File', self) |
---|
84 | self.file_menu.addAction('&Quit', self.fileQuit, |
---|
85 | QtCore.Qt.CTRL + QtCore.Qt.Key_Q) |
---|
86 | self.menuBar().addMenu(self.file_menu) |
---|
87 | |
---|
88 | self.help_menu = QtGui.QMenu('&Help', self) |
---|
89 | self.menuBar().addSeparator() |
---|
90 | self.menuBar().addMenu(self.help_menu) |
---|
91 | |
---|
92 | self.help_menu.addAction('&About', self.about) |
---|
93 | |
---|
94 | self.main_widget = QtGui.QWidget(self) |
---|
95 | |
---|
96 | l = QtGui.QVBoxLayout(self.main_widget) |
---|
97 | sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100) |
---|
98 | dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100) |
---|
99 | l.addWidget(sc) |
---|
100 | l.addWidget(dc) |
---|
101 | |
---|
102 | self.main_widget.setFocus() |
---|
103 | self.setCentralWidget(self.main_widget) |
---|
104 | |
---|
105 | self.statusBar().showMessage("All hail matplotlib!", 2000) |
---|
106 | |
---|
107 | def fileQuit(self): |
---|
108 | self.close() |
---|
109 | |
---|
110 | def closeEvent(self, ce): |
---|
111 | self.fileQuit() |
---|
112 | |
---|
113 | def about(self): |
---|
114 | QtGui.QMessageBox.about(self, "About", |
---|
115 | """embedding_in_qt4.py example |
---|
116 | Copyright 2005 Florent Rougon, 2006 Darren Dale |
---|
117 | |
---|
118 | This program is a simple example of a Qt4 application embedding matplotlib |
---|
119 | canvases. |
---|
120 | |
---|
121 | It may be used and modified with no restriction; raw copies as well as |
---|
122 | modified versions may be distributed without limitation.""" |
---|
123 | ) |
---|
124 | |
---|
125 | |
---|
126 | qApp = QtGui.QApplication(sys.argv) |
---|
127 | |
---|
128 | aw = ApplicationWindow() |
---|
129 | aw.setWindowTitle("%s" % progname) |
---|
130 | aw.show() |
---|
131 | sys.exit(qApp.exec_()) |
---|
132 | #qApp.exec_() |
---|