利用PyQt5+Matplotlib 绘制静态/动态图

本文介绍如何使用PyQt5和Matplotlib在Windows环境下创建GUI应用,实现静态和动态数据可视化。关键在于将matplotlib图形类Myplot继承自FigureCanvas,使图形成为PyQt控件。通过NavigationToolbar实现基本的图像操作,并通过定时器动态更新数据,展示数据实时采集和显示的功能。文章还提及了未来可能通过多线程优化功能。
摘要由CSDN通过智能技术生成

代码编辑环境

Win10+(Pycharmm or Vscode)+PyQt 5.14.2

功能实现

静态作图:数据作图,取决于作图函数,可自行修改
动态作图:产生数据,获取并更新数据,最后刷新显示,可用于实现数据实时采集并显示的场景

效果展示

在这里插入图片描述

代码块(业务与逻辑分离)

业务–UI界面代码

文件名:Ui_realtimer_plot.py

# -*- coding: utf-8 -*-
# Added by the Blog author VERtiCaL on 2020/07/12 at SSRF
# Created by: PyQt5 UI code generator 5.14.2
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1613, 1308)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.Plot_static = QtWidgets.QGroupBox(self.centralwidget)
        self.Plot_static.setGeometry(QtCore.QRect(260, 30, 861, 391))
        self.Plot_static.setObjectName("Plot_static")
        self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QtCore.QRect(300, 830, 701, 91))
        self.layoutWidget.setObjectName("layoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setSpacing(28)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.Static_plot = QtWidgets.QPushButton(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.MinimumExpanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.Static_plot.sizePolicy().hasHeightForWidth())
        self.Static_plot.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("楷体")
        font.setPointSize(18)
        font.setBold(False)
        font.setWeight(50)
        self.Static_plot.setFont(font)
        self.Static_plot.setObjectName("Static_plot")
        self.horizontalLayout.addWidget(self.Static_plot)
        self.dynamic_plot = QtWidgets.QPushButton(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.MinimumExpanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.dynamic_plot.sizePolicy().hasHeightForWidth())
        self.dynamic_plot.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("楷体")
        font.setPointSize(18)
        font.setBold(False)
        font.setWeight(50)
        self.dynamic_plot.setFont(font)
        self.dynamic_plot.setObjectName("dynamic_plot")
        self.horizontalLayout.addWidget(self.dynamic_plot)
        self.End_plot = QtWidgets.QPushButton(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.MinimumExpanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.End_plot.sizePolicy().hasHeightForWidth())
        self.End_plot.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("楷体")
        font.setPointSize(18)
        self.End_plot.setFont(font)
        self.End_plot.setObjectName("End_plot")
        self.horizontalLayout.addWidget(self.End_plot)
        self.Erase_plot = QtWidgets.QPushButton(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.MinimumExpanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.Erase_plot
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值