1
1
"""
2
- ================
3
- Embedding In QT5
4
- ================
5
-
6
- Simple Qt5 application embedding Matplotlib canvases
7
-
8
- Copyright (C) 2005 Florent Rougon
9
- 2006 Darren Dale
10
- 2015 Jens H Nielsen
11
-
12
- This file is an example program for Matplotlib. It may be used and
13
- modified with no restriction; raw copies as well as modified versions
14
- may be distributed without limitation.
2
+ ===============
3
+ Embedding In Qt
4
+ ===============
5
+
6
+ Simple Qt application embedding Matplotlib canvases. This program will work
7
+ equally well using Qt4 and Qt5. Either version of Qt can be selected (for
8
+ example) by setting the ``MPLBACKEND`` environment variable to "Qt4Agg" or
9
+ "Qt5Agg", or by first importing the desired version of PyQt.
15
10
"""
16
11
17
12
from __future__ import unicode_literals
18
- import sys
13
+
19
14
import os
20
- import random
21
- import matplotlib
22
- # Make sure that we are using QT5
23
- matplotlib .use ('Qt5Agg' )
24
- # Uncomment this line before running, it breaks sphinx-gallery builds
25
- # from PyQt5 import QtCore, QtWidgets
26
-
27
- from numpy import arange , sin , pi
28
- from matplotlib .backends .backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
15
+ import sys
16
+
17
+ import numpy as np
18
+
19
+ from matplotlib .backends .qt_compat import QtCore , QtWidgets , is_pyqt5
20
+ if is_pyqt5 ():
21
+ from matplotlib .backends .backend_qt5agg import (
22
+ FigureCanvas , NavigationToolbar2QT as NavigationToolbar )
23
+ else :
24
+ from matplotlib .backends .backend_qt4agg import (
25
+ FigureCanvas , NavigationToolbar2QT as NavigationToolbar )
29
26
from matplotlib .figure import Figure
30
27
28
+
31
29
progname = os .path .basename (sys .argv [0 ])
32
- progversion = "0.1"
33
30
34
31
35
32
class MyMplCanvas (FigureCanvas ):
36
33
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
37
34
38
35
def __init__ (self , parent = None , width = 5 , height = 4 , dpi = 100 ):
39
36
fig = Figure (figsize = (width , height ), dpi = dpi )
40
- self . axes = fig . add_subplot ( 111 )
37
+ FigureCanvas . __init__ ( self , fig )
41
38
39
+ self .axes = fig .subplots ()
42
40
self .compute_initial_figure ()
43
41
44
- FigureCanvas .__init__ (self , fig )
45
42
self .setParent (parent )
46
-
47
- FigureCanvas .setSizePolicy (self ,
48
- QtWidgets .QSizePolicy .Expanding ,
49
- QtWidgets .QSizePolicy .Expanding )
50
- FigureCanvas .updateGeometry (self )
43
+ self .setSizePolicy (QtWidgets .QSizePolicy .Expanding ,
44
+ QtWidgets .QSizePolicy .Expanding )
45
+ self .updateGeometry ()
51
46
52
47
def compute_initial_figure (self ):
53
48
pass
@@ -57,8 +52,8 @@ class MyStaticMplCanvas(MyMplCanvas):
57
52
"""Simple canvas with a sine plot."""
58
53
59
54
def compute_initial_figure (self ):
60
- t = arange (0.0 , 3.0 , 0.01 )
61
- s = sin (2 * pi * t )
55
+ t = np . arange (0.0 , 3.0 , 0.01 )
56
+ s = np . sin (2 * np . pi * t )
62
57
self .axes .plot (t , s )
63
58
64
59
@@ -75,10 +70,9 @@ def compute_initial_figure(self):
75
70
self .axes .plot ([0 , 1 , 2 , 3 ], [1 , 2 , 0 , 4 ], 'r' )
76
71
77
72
def update_figure (self ):
78
- # Build a list of 4 random integers between 0 and 10 (both inclusive)
79
- l = [random .randint (0 , 10 ) for i in range (4 )]
80
73
self .axes .cla ()
81
- self .axes .plot ([0 , 1 , 2 , 3 ], l , 'r' )
74
+ # Plot 4 random integers between 0 and 9.
75
+ self .axes .plot (range (4 ), np .random .randint (0 , 10 , 4 ), 'r' )
82
76
self .draw ()
83
77
84
78
@@ -107,10 +101,12 @@ def __init__(self):
107
101
l .addWidget (sc )
108
102
l .addWidget (dc )
109
103
110
- self .main_widget .setFocus ()
111
104
self .setCentralWidget (self .main_widget )
112
105
113
- self .statusBar ().showMessage ("All hail matplotlib!" , 2000 )
106
+ # Add a toolbar for each canvas.
107
+ self .addToolBar (NavigationToolbar (sc , self ))
108
+ self .addToolBar (
109
+ QtCore .Qt .BottomToolBarArea , NavigationToolbar (dc , self ))
114
110
115
111
def fileQuit (self ):
116
112
self .close ()
@@ -119,25 +115,16 @@ def closeEvent(self, ce):
119
115
self .fileQuit ()
120
116
121
117
def about (self ):
122
- QtWidgets .QMessageBox .about (self , "About" ,
123
- """embedding_in_qt5.py example
124
- Copyright 2005 Florent Rougon, 2006 Darren Dale, 2015 Jens H Nielsen
125
-
126
- This program is a simple example of a Qt5 application embedding matplotlib
127
- canvases.
128
-
129
- It may be used and modified with no restriction; raw copies as well as
130
- modified versions may be distributed without limitation.
118
+ QtWidgets .QMessageBox .about (self , "About" , """\
119
+ embedding_in_qt_sgskip.py example
131
120
132
- This is modified from the embedding in qt4 example to show the difference
133
- between qt4 and qt5"""
134
- )
121
+ This program is a simple example of a Qt application embedding Matplotlib \
122
+ canvases.""" )
135
123
136
124
137
125
qApp = QtWidgets .QApplication (sys .argv )
138
126
139
127
aw = ApplicationWindow ()
140
- aw .setWindowTitle ("%s" % progname )
128
+ aw .setWindowTitle (progname )
141
129
aw .show ()
142
130
sys .exit (qApp .exec_ ())
143
- #qApp.exec_()
0 commit comments