利用PyQt5+Matplotlib 绘制静态/动态图
代码编辑环境
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