Skip to content

Commit 37e600e

Browse files
committed
Added a demo-file to show how to use the FigureManager classes to
get pop-up graph windows as part of a larger gui application.
1 parent 3014931 commit 37e600e

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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, division
13+
import sys
14+
import os
15+
16+
from matplotlib.backends.qt4_compat import QtCore, QtGui
17+
import numpy as np
18+
from matplotlib.backends._backend_qt4agg import (FigureCanvasQTAgg,
19+
FigureManagerQTAgg,
20+
new_figure_manager)
21+
22+
from matplotlib.figure import Figure
23+
24+
progname = os.path.basename(sys.argv[0])
25+
progversion = "0.1"
26+
27+
28+
def demo_plot(ax):
29+
"""
30+
Plots sin waves with random period
31+
"""
32+
k = np.random.random_integers(1, 10)
33+
th = np.linspace(0, 2*np.pi, 1024)
34+
ax.plot(th, np.sin(th * k))
35+
36+
37+
class ApplicationWindow(QtGui.QMainWindow):
38+
def __init__(self):
39+
# QT boiler plate to set up main window
40+
QtGui.QMainWindow.__init__(self)
41+
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
42+
self.setWindowTitle("application main window")
43+
44+
self.file_menu = QtGui.QMenu('&File', self)
45+
self.file_menu.addAction('&Quit', self.fileQuit,
46+
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
47+
self.menuBar().addMenu(self.file_menu)
48+
49+
self.help_menu = QtGui.QMenu('&Help', self)
50+
self.menuBar().addSeparator()
51+
self.menuBar().addMenu(self.help_menu)
52+
53+
self.help_menu.addAction('&About', self.about)
54+
55+
self.main_widget = QtGui.QWidget(self)
56+
57+
l = QtGui.QVBoxLayout(self.main_widget)
58+
button = QtGui.QPushButton("make window A")
59+
button.clicked.connect(self.new_window_hard_way)
60+
l.addWidget(button)
61+
62+
buttonB = QtGui.QPushButton("make window B")
63+
buttonB.clicked.connect(self.new_window_easy_way)
64+
l.addWidget(buttonB)
65+
self.main_widget.setFocus()
66+
self.setCentralWidget(self.main_widget)
67+
self._figs = []
68+
self.statusBar().showMessage("All hail matplotlib!", 2000)
69+
70+
def new_window_hard_way(self):
71+
# make a figure
72+
fig = Figure()
73+
# make a canvas
74+
canvas = FigureCanvasQTAgg(fig)
75+
# make a manager from the canvas
76+
manager = FigureManagerQTAgg(canvas, 1)
77+
# grab an axes in the figure
78+
ax = fig.gca()
79+
# plot some demo code
80+
demo_plot(ax)
81+
# show the window
82+
manager.show()
83+
84+
def new_window_easy_way(self):
85+
# make a new manager
86+
manager = new_figure_manager(2)
87+
# grab a reference to the figure
88+
fig = manager.canvas.figure
89+
# get an axes object in the figure
90+
ax = fig.gca()
91+
# plot some demo-data
92+
demo_plot(ax)
93+
# show the window
94+
manager.show()
95+
96+
def fileQuit(self):
97+
self.close()
98+
99+
def closeEvent(self, ce):
100+
self.fileQuit()
101+
102+
def about(self):
103+
QtGui.QMessageBox.about(self, "About",
104+
"""embedding_in_qt4.py example
105+
Copyright 2005 Florent Rougon, 2006 Darren Dale, 2013 Thomas Caswell
106+
107+
This program is a simple example of a Qt4 application making use of
108+
the FigureManager class.
109+
110+
It may be used and modified with no restriction; raw copies as well as
111+
modified versions may be distributed without limitation."""
112+
)
113+
114+
115+
qApp = QtGui.QApplication(sys.argv)
116+
117+
aw = ApplicationWindow()
118+
aw.setWindowTitle("%s" % progname)
119+
aw.show()
120+
sys.exit(qApp.exec_())
121+
#qApp.exec_()

0 commit comments

Comments
 (0)