稍微复杂地实现matplotlib绑定到PyQt5(有菜单)
【知识点】
import matplotlib matplotlib.use("Qt5Agg")
【效果图】
【源代码】
1 import sys 2 import random 3 4 import matplotlib 5 matplotlib.use("Qt5Agg") 6 7 from PyQt5 import QtCore 8 from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget 9 10 from numpy import arange, sin, pi 11 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 12 from matplotlib.figure import Figure 13 14 15 class MyMplCanvas(FigureCanvas): 16 """这是一个窗口部件,即QWidget(当然也是FigureCanvasAgg)""" 17 def __init__(self, parent=None, width=5, height=4, dpi=100): 18 fig = Figure(figsize=(width, height), dpi=dpi) 19 self.axes = fig.add_subplot(111) 20 # 每次plot()调用的时候,我们希望原来的坐标轴被清除(所以False) 21 self.axes.hold(False) 22 23 self.compute_initial_figure() 24 25 # 26 FigureCanvas.__init__(self, fig) 27 self.setParent(parent) 28 29 FigureCanvas.setSizePolicy(self, 30 QSizePolicy.Expanding, 31 QSizePolicy.Expanding) 32 FigureCanvas.updateGeometry(self) 33 34 def compute_initial_figure(self): 35 pass 36 37 class MyStaticMplCanvas(MyMplCanvas): 38 """静态画布:一条正弦线""" 39 def compute_initial_figure(self): 40 t = arange(0.0, 3.0, 0.01) 41 s = sin(2*pi*t) 42 self.axes.plot(t, s) 43 44 45 class MyDynamicMplCanvas(MyMplCanvas): 46 """动态画布:每秒自动更新,更换一条折线。""" 47 def __init__(self, *args, **kwargs): 48 MyMplCanvas.__init__(self, *args, **kwargs) 49 timer = QtCore.QTimer(self) 50 timer.timeout.connect(self.update_figure) 51 timer.start(1000) 52 53 def compute_initial_figure(self): 54 self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], ‘r‘) 55 56 def update_figure(self): 57 # 构建4个随机整数,位于闭区间[0, 10] 58 l = [random.randint(0, 10) for i in range(4)] 59 60 self.axes.plot([0, 1, 2, 3], l, ‘r‘) 61 self.draw() 62 63 class ApplicationWindow(QMainWindow): 64 def __init__(self): 65 QMainWindow.__init__(self) 66 self.setAttribute(QtCore.Qt.WA_DeleteOnClose) 67 self.setWindowTitle("程序主窗口") 68 69 self.file_menu = QMenu(‘&File‘, self) 70 self.file_menu.addAction(‘&Quit‘, self.fileQuit, 71 QtCore.Qt.CTRL + QtCore.Qt.Key_Q) 72 self.menuBar().addMenu(self.file_menu) 73 74 self.help_menu = QMenu(‘&Help‘, self) 75 self.menuBar().addSeparator() 76 self.menuBar().addMenu(self.help_menu) 77 78 self.help_menu.addAction(‘&About‘, self.about) 79 80 self.main_widget = QWidget(self) 81 82 l = QVBoxLayout(self.main_widget) 83 sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100) 84 dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100) 85 l.addWidget(sc) 86 l.addWidget(dc) 87 88 self.main_widget.setFocus() 89 self.setCentralWidget(self.main_widget) 90 # 状态条显示2秒 91 self.statusBar().showMessage("matplotlib 万岁!", 2000) 92 93 def fileQuit(self): 94 self.close() 95 96 def closeEvent(self, ce): 97 self.fileQuit() 98 99 def about(self): 100 QMessageBox.about(self, "About", 101 """embedding_in_qt5.py example 102 Copyright 2015 BoxControL 103 104 This program is a simple example of a Qt5 application embedding matplotlib 105 canvases. It is base on example from matplolib documentation, and initially was 106 developed from Florent Rougon and Darren Dale. 107 108 http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html 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 if __name__ == ‘__main__‘: 116 app = QApplication(sys.argv) 117 118 aw = ApplicationWindow() 119 aw.setWindowTitle("PyQt5 与 Matplotlib 例子") 120 aw.show() 121 #sys.exit(qApp.exec_()) 122 app.exec_()
时间: 2024-10-23 15:19:16