当前位置:网站首页>PyQt5快速开发与实战 9.4 Matplotlib在PyQt中的应用
PyQt5快速开发与实战 9.4 Matplotlib在PyQt中的应用
2022-07-31 09:06:00 【Ding Jiaxiong】
PyQt5快速开发与实战
文章目录
9. 第9章 PyQt5 扩展应用
9.4 Matplotlib在PyQt中的应用
Matplotlib是 Python最著名的绘图库,它提供了一整套和 MATLAB相似的命令API,十分适合交互式进行制图。而且也可以方便地将它作为绘图控件,嵌入到GUI应用程序中。
9.4.1 对MatplotlibWidget的解读
设置绘图类
class MyMplCanvas(FigureCanvas): """FigureCanvas的最终的父类其实是QWidget。""" def __init__(self, parent=None, width=5, height=4, dpi=100): # 配置中文显示 plt.rcParams['font.family'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 self.fig = Figure(figsize=(width, height), dpi=dpi) # 新建一个figure self.axes = self.fig.add_subplot(111) # 建立一个子图,如果要建立复合图,可以在这里修改 self.axes.hold(False) # 每次绘图的时候不保留上一次绘图的结果 FigureCanvas.__init__(self, self.fig) self.setParent(parent) '''定义FigureCanvas的尺寸策略,这部分的意思是设置FigureCanvas,使之尽可能的向外填充空间。''' FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(self)
定义绘制静态图函数(自定义)
'''绘制静态图,可以在这里定义自己的绘图逻辑''' def start_static_plot(self): self.fig.suptitle('测试静态图') t = arange(0.0, 3.0, 0.01) s = sin(2 * pi * t) self.axes.plot(t, s) self.axes.set_ylabel('静态图:Y轴') self.axes.set_xlabel('静态图:X轴') self.axes.grid(True)
定义绘制动态图函数,设置每隔1秒重新绘制一次图像。
'''启动绘制动态图''' def start_dynamic_plot(self, *args, **kwargs): timer = QtCore.QTimer(self) timer.timeout.connect(self.update_figure) # 每隔一段时间就会触发一次update_figure函数。 timer.start(1000) # 触发的时间间隔为1秒。 '''动态图的绘图逻辑可以在这里修改''' def update_figure(self): self.fig.suptitle('测试动态图') l = [random.randint(0, 10) for i in range(4)] self.axes.plot([0, 1, 2, 3], l, 'r') self.axes.set_ylabel('动态图:Y轴') self.axes.set_xlabel('动态图:X轴') self.axes.grid(True) self.draw()
封装绘图类
这部分主要是使用 QWidget 把上面的绘图类和工具栏封装到MatplotlibWidget中,只需调用 MatplotlibWidget这个类就可以实现绘图功能了。
class MatplotlibWidget(QWidget):
def __init__(self, parent=None):
super(MatplotlibWidget, self).__init__(parent)
self.initUi()
def initUi(self):
self.layout = QVBoxLayout(self)
self.mpl = MyMplCanvas(self, width=5, height=4, dpi=100)
# self.mpl.start_static_plot() # 如果你想要初始化的时候就呈现静态图,请把这行注释去掉
#self.mpl.start_dynamic_plot() # 如果你想要初始化的时候就呈现动态图,请把这行注释去掉
self.mpl_ntb = NavigationToolbar(self.mpl, self) # 添加完整的 toolbar
self.layout.addWidget(self.mpl)
self.layout.addWidget(self.mpl_ntb)
测试程序
野生的错误出现了
直接把这行注掉
9.4.2 设置提升的窗口控件
新建QWidget类,并将其提升
对窗口进行布局设置
编辑窗口,ui转py
这里可以发现出错了,把前一文件名改一下
9.4.3 MatplotlibWidget的使用
初始化模型并主函数调用
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication
from matplotlib_pyqt import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
""" Class documentation goes here. """
def __init__(self, parent=None):
""" Constructor @param parent reference to the parent widget @type QWidget """
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.matplotlibwidget_dynamic.setVisible(False)
self.matplotlibwidget_static.setVisible(False)
@pyqtSlot()
def on_pushButton_clicked(self):
""" Slot documentation goes here. """
self.matplotlibwidget_static.setVisible(True)
self.matplotlibwidget_static.mpl.start_static_plot()
@pyqtSlot()
def on_pushButton_2_clicked(self):
""" Slot documentation goes here. """
self.matplotlibwidget_dynamic.setVisible(True)
self.matplotlibwidget_dynamic.mpl.start_dynamic_plot()
if __name__ == "__main__":
import sys
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec_())
9.4.4 更多扩展
https://matplotlib.org/stable/gallery/index.html
根据示例代码修改MyMplCanvas类,实现更多交互。
边栏推荐
猜你喜欢
Splunk Workflow action 给我们带来的好处
MySQL 视图(详解)
js department budget and expenditure radar chart
[MySQL exercises] Chapter 2 Basic operations of databases and data tables
[MySQL exercises] Chapter 4 · Explore operators in MySQL with kiko
[Cloud native and 5G] Microservices support 5G core network
ONES 入选 CFS 财经峰会「2022数字化创新引领奖」
35-Jenkins-共享库应用
ARC在编译和运行做了什么?
I advise those juniors and juniors who have just started working: If you want to enter a big factory, you must master these core skills!Complete Learning Route!
随机推荐
Canvas particles change various shapes js special effects
ScheduledExecutorService - 定时周期执行任务
How to restore data using mysql binlog
【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符
[Cloud native] Introduction and use of Feign of microservices
ecshop安装的时候提示不支持JPEG格式
基于学生成绩管理系统(附源代码及数据库)
Browser usage ratio js radar chart
【TCP/IP】Network Model
云服务器部署 Web 项目
关于挂载EXfat文件格式U盘失败的问题
The future of the hybrid interface: conversational UI
Define event types in Splunk Web
[MySQL exercises] Chapter 3 Common data types in MySQL
【TCP/IP】网络模型
MySQL----多表查询
qt在不同的线程中传递自定义结构体参数
postgresql 生成随机日期,随机时间
高并发-高可用-高性能
来n遍剑指--09. 用两个栈实现队列