-
Notifications
You must be signed in to change notification settings - Fork 1
/
03-matplotlib_pyqt.py
77 lines (65 loc) · 2.63 KB
/
03-matplotlib_pyqt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
From https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-qt-sgskip-py
Changed 'subplots()' to 'add_subplot(111)' to work with older matplotlib versions
===============
Embedding in Qt
===============
Simple Qt application embedding Matplotlib canvases. This program will work
equally well using Qt4 and Qt5. Either version of Qt can be selected (for
example) by setting the ``MPLBACKEND`` environment variable to "Qt4Agg" or
"Qt5Agg", or by first importing the desired version of PyQt.
"""
import sys
import time
import numpy as np
from matplotlib.backends.qt_compat import QtCore, QtWidgets
try:
from matplotlib.backends.qt_compat import is_pyqt5
except ImportError:
def is_pyqt5():
from matplotlib.backends.qt_compat import QT_API
return QT_API == u'PyQt5'
if is_pyqt5():
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
else:
from matplotlib.backends.backend_qt4agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
static_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(static_canvas)
self.addToolBar(NavigationToolbar(static_canvas, self))
self._static_ax = static_canvas.figure.add_subplot(111)
t = np.linspace(0, 10, 501)
self._static_ax.plot(t, np.tan(t), ".")
'''
dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(dynamic_canvas)
self.addToolBar(QtCore.Qt.BottomToolBarArea,
NavigationToolbar(dynamic_canvas, self))
self._dynamic_ax = dynamic_canvas.figure.add_subplot(111)
self._timer = dynamic_canvas.new_timer(
100, [(self._update_canvas, (), {})])
self._timer.start()
def _update_canvas(self):
self._dynamic_ax.clear()
t = np.linspace(0, 10, 101)
# Shift the sinusoid as a function of time.
self._dynamic_ax.plot(t, np.sin(t + time.time()))
self._dynamic_ax.figure.canvas.draw()
'''
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
'''
Note that the pyqt now has control of communication. Try adding grid, or transform
the x or y axis from linear to logarithmic
'''