Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b93410

Browse files
committed
Unify the three Qt5 embedding examples.
... and modernize them a bit.
1 parent 3c3e8ba commit 0b93410

File tree

4 files changed

+44
-268
lines changed

4 files changed

+44
-268
lines changed

examples/user_interfaces/embedding_in_qt4_sgskip.py

Lines changed: 0 additions & 138 deletions
This file was deleted.

examples/user_interfaces/embedding_in_qt4_wtoolbar_sgskip.py

Lines changed: 0 additions & 73 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
11
"""
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.
1510
"""
1611

1712
from __future__ import unicode_literals
18-
import sys
13+
1914
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)
2926
from matplotlib.figure import Figure
3027

28+
3129
progname = os.path.basename(sys.argv[0])
32-
progversion = "0.1"
3330

3431

3532
class MyMplCanvas(FigureCanvas):
3633
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
3734

3835
def __init__(self, parent=None, width=5, height=4, dpi=100):
3936
fig = Figure(figsize=(width, height), dpi=dpi)
40-
self.axes = fig.add_subplot(111)
37+
FigureCanvas.__init__(self, fig)
4138

39+
self.axes = fig.subplots()
4240
self.compute_initial_figure()
4341

44-
FigureCanvas.__init__(self, fig)
4542
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()
5146

5247
def compute_initial_figure(self):
5348
pass
@@ -57,8 +52,8 @@ class MyStaticMplCanvas(MyMplCanvas):
5752
"""Simple canvas with a sine plot."""
5853

5954
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)
6257
self.axes.plot(t, s)
6358

6459

@@ -75,10 +70,9 @@ def compute_initial_figure(self):
7570
self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')
7671

7772
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)]
8073
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')
8276
self.draw()
8377

8478

@@ -107,10 +101,12 @@ def __init__(self):
107101
l.addWidget(sc)
108102
l.addWidget(dc)
109103

110-
self.main_widget.setFocus()
111104
self.setCentralWidget(self.main_widget)
112105

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))
114110

115111
def fileQuit(self):
116112
self.close()
@@ -119,25 +115,16 @@ def closeEvent(self, ce):
119115
self.fileQuit()
120116

121117
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
131120
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.""")
135123

136124

137125
qApp = QtWidgets.QApplication(sys.argv)
138126

139127
aw = ApplicationWindow()
140-
aw.setWindowTitle("%s" % progname)
128+
aw.setWindowTitle(progname)
141129
aw.show()
142130
sys.exit(qApp.exec_())
143-
#qApp.exec_()

tutorials/introductory/sample_plots.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,11 @@
397397
398398
For examples of how to embed Matplotlib in different toolkits, see:
399399
400-
* :ref:`sphx_glr_gallery_user_interfaces_embedding_in_gtk2_sgskip.py`
401-
* :ref:`sphx_glr_gallery_user_interfaces_embedding_in_wx2_sgskip.py`
402-
* :ref:`sphx_glr_gallery_user_interfaces_mpl_with_glade_sgskip.py`
403-
* :ref:`sphx_glr_gallery_user_interfaces_embedding_in_qt4_sgskip.py`
404-
* :ref:`sphx_glr_gallery_user_interfaces_embedding_in_tk_sgskip.py`
400+
* :doc:`/gallery/user_interfaces/embedding_in_gtk2_sgskip`
401+
* :doc:`/gallery/user_interfaces/embedding_in_wx2_sgskip`
402+
* :doc:`/gallery/user_interfaces/mpl_with_glade_sgskip`
403+
* :doc:`/gallery/user_interfaces/embedding_in_qt_sgskip`
404+
* :doc:`/gallery/user_interfaces/embedding_in_tk_sgskip`
405405
406406
XKCD-style sketch plots
407407
=======================

0 commit comments

Comments
 (0)