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

本文共 4277 字,大约阅读时间需要 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://www.cnblogs.com/hhh5460/p/4322652.html
,如需转载请自行联系原作者
你可能感兴趣的文章
mysql5.7 创建一个超级管理员
查看>>
【框架整合】Maven-SpringMVC3.X+Spring3.X+MyBatis3-日志、JSON解析、表关联查询等均已配置好...
查看>>
要想成为高级Java程序员需要具备哪些知识呢?
查看>>
带着问题去学习--Nginx配置解析(一)
查看>>
onix-文件系统
查看>>
java.io.Serializable浅析
查看>>
我的友情链接
查看>>
多线程之线程池任务管理通用模板
查看>>
CSS3让长单词与URL地址自动换行——word-wrap属性
查看>>
CodeForces 580B Kefa and Company
查看>>
开发规范浅谈
查看>>
Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming
查看>>
鼠标增强软件StrokeIt使用方法
查看>>
本地连接linux虚拟机的方法
查看>>
某公司面试java试题之【二】,看看吧,说不定就是你将要做的题
查看>>
BABOK - 企业分析(Enterprise Analysis)概要
查看>>
Linux 配置vnc,开启linux远程桌面
查看>>
CentOS6.4关闭触控板
查看>>
React Native 极光推送填坑(ios)
查看>>
Terratest:一个用于自动化基础设施测试的开源Go库
查看>>