博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
matplotlib绑定到PyQt5(有菜单)
阅读量:5879 次
发布时间:2019-06-19

本文共 4200 字,大约阅读时间需要 14 分钟。

稍微复杂地实现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 example102         Copyright 2015 BoxControL103 104         This program is a simple example of a Qt5 application embedding matplotlib105         canvases. It is base on example from matplolib documentation, and initially was106         developed from Florent Rougon and Darren Dale.107 108         http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html109 110         It may be used and modified with no restriction; raw copies as well as111         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_()

 

转载地址:http://ibdix.baihongyu.com/

你可能感兴趣的文章
新手学JavaScript都要学什么?
查看>>
湖南省第九届大学生计算机程序设计竞赛 搞笑版费马大定理
查看>>
梦想永远那么近——《白箱》后日谈
查看>>
Activity启动过程源代码分析
查看>>
python调用shell命令之三慷慨法
查看>>
J2EE基础总结(5)——EJB
查看>>
优秀的相关站点留存-不定时更新
查看>>
.net中webconfig自定义配置
查看>>
【数据结构和算法16】堆排序
查看>>
PHP实现连接设备、通讯和发送命令的方法
查看>>
【HDOJ 5379】 Mahjong tree
查看>>
iOS UITableView表视图滚动隐藏UINavigationController导航栏
查看>>
SDL如何嵌入到QT中?!
查看>>
$(document).ready()
查看>>
RunLoop总结:RunLoop的应用场景(四)
查看>>
8个很实用的在线工具来提高你的Web设计和开发能力
查看>>
P1026 统计单词个数
查看>>
AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe...
查看>>
MySQL主从同步那点事儿
查看>>
Python RGB 和HSV颜色相互转换
查看>>