利用PyQt5+Matplotlib 绘制静态/动态图的实现代码


Posted in Python onJuly 13, 2020

代码编辑环境

Win10+(Pycharmm or Vscode)+PyQt 5.14.2

功能实现

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

效果展示

利用PyQt5+Matplotlib 绘制静态/动态图的实现代码

代码块(业务与逻辑分离)业务?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.sizePolicy().hasHeightForWidth())
    self.Erase_plot.setSizePolicy(sizePolicy)
    font = QtGui.QFont()
    font.setFamily("楷体")
    font.setPointSize(18)
    self.Erase_plot.setFont(font)
    self.Erase_plot.setObjectName("Erase_plot")
    self.horizontalLayout.addWidget(self.Erase_plot)
    self.Plot_dynamic = QtWidgets.QGroupBox(self.centralwidget)
    self.Plot_dynamic.setGeometry(QtCore.QRect(260, 430, 861, 391))
    self.Plot_dynamic.setObjectName("Plot_dynamic")
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 1613, 23))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

  def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.Plot_static.setTitle(_translate("MainWindow", "StaticPlot"))
    self.Static_plot.setText(_translate("MainWindow", "静态作图"))
    self.dynamic_plot.setText(_translate("MainWindow", "动态作图"))
    self.End_plot.setText(_translate("MainWindow", "停止作图"))
    self.Erase_plot.setText(_translate("MainWindow", "清除数据"))
    self.Plot_dynamic.setTitle(_translate("MainWindow", "DynamicPlot"))

逻辑?主要代码分析

matplotlib作图嵌入PyQt界面的关键

创建matlibplot图形类Myplot,通过继承FigureCanvas类,使其相当于PyQt里的控件,从而完成PyQt与Matlibplot的结合。

# class Myplot for plotting with matplotlib
class Myplot(FigureCanvas):
  def __init__(self, parent=None, width=5, height=3, dpi=100):
    # normalized for 中文显示和负号
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False

    # new fig
    self.fig = Figure(figsize=(width, height), dpi=dpi)
    # activate figure window
    # super(Plot_dynamic,self).__init__(self.fig)
    FigureCanvas.__init__(self, self.fig)
    self.setParent(parent)
    # sub plot by self.axes
    self.axes= self.fig.add_subplot(111)
    # initial figure
    self.compute_initial_figure()

    # size policy
    FigureCanvas.setSizePolicy(self,
                  QtWidgets.QSizePolicy.Expanding,
                  QtWidgets.QSizePolicy.Expanding)
    FigureCanvas.updateGeometry(self)

  def compute_initial_figure(self):
    pass

用于图形初始化的图像类,通过调用这个类就能实现图形绘制和修改。可以在此更改图形的类型,具体代码可以参照matplotlib官网的实例 Matplotlib_examples

class static_fig(Myplot):
  def __init__(self,*args,**kwargs):
    Myplot.__init__(self,*args,**kwargs)

  def compute_initial_figure(self):
    x=np.linspace(0,2*np.pi,100)
    y=x*np.sin(x)
    self.axes.plot(x,y)
    self.axes.set_title("signals")
    self.axes.set_xlabel("delay(s)")
    self.axes.set_ylabel("counts")

主界面的逻辑代码

几点说明

1、利用Matplotlib自带的NavigationToolbar可以实现绘制图的基本操作:平移、放大、保存图像、显示鼠标位置(x,y)的数据等
2、self.gridlayout1.addWidget(self.fig1)就是把绘制的图像本身作为一个控件widget加入UI界面里的groupbox(这里改成Plot_static名称)去,从而使得图形能正常显示在绘图框里。

class AppWindow(QMainWindow,Ui_MainWindow):
  def __init__(self,parent=None):
    super(AppWindow,self).__init__(parent)
    self.setupUi(self)
    # ^O^ static_fig can changed to any other function
    #self.fig1=static_fig(width=5, height=4, dpi=100)
    self.fig1 = static_fig(width=5, height=3, dpi=72)
    self.fig2 = dynamic_fig(width=5, height=3, dpi=72)
    # add NavigationToolbar in the figure (widgets)
    self.fig_ntb1 = NavigationToolbar(self.fig1, self)
    self.fig_ntb2 = NavigationToolbar(self.fig2, self)
    #self.Start_plot.clicked.connect(self.plot_cos)
    # add the static_fig in the Plot box
    self.gridlayout1=QGridLayout(self.Plot_static)
    self.gridlayout1.addWidget(self.fig1)
    self.gridlayout1.addWidget(self.fig_ntb1)
    # add the dynamic_fig in the Plot box
    self.gridlayout2 = QGridLayout(self.Plot_dynamic)
    self.gridlayout2.addWidget(self.fig2)
    self.gridlayout2.addWidget(self.fig_ntb2)
    self._timer = QTimer(self)
    self._t = 1
    self._counts = []
    self._delay_t = []

静态做图

self.fig1.axes.cla()清除原来的图像,self.fig1.axes.plot(self.t,self.y),通过self.fig1.axes.plot实现做图,不同类型的图形做图参考matplotlib官网。 Matplotlib_examples

@pyqtSlot()
  def on_Static_plot_clicked(self):
    self.plot_cos()
    self._Static_on=1
    #self.Start_plot.setEnabled(False)

  global nc
  nc=1
  def plot_cos(self):
    #print('nc=%d\n' %self.nc)
    global nc
    nc+=1
    self.fig1.axes.cla()
    self.t=np.arange(0,15,0.1)
    self.y=2*nc*self.t-self.t*np.cos(self.t/2/np.pi*1000)
    self.fig1.axes.plot(self.t,self.y)
    self.fig1.axes.set_title("signals",fontsize=18,color='c')
    self.fig1.axes.set_xlabel("delay(s)",fontsize=18,color='c')
    self.fig1.axes.set_ylabel("counts",fontsize=18,color='c')
    self.fig1.draw()

动态做图

这里数据接收通过QTimer来延迟时间(隔1s)并通过函数产生计数,append更新数据,做图,刷新图像,self.fig2.draw()实现图像绘制。

@pyqtSlot()
  def on_dynamic_plot_clicked(self):
    print('start dynamic ploting')
    self.Static_plot.setEnabled(False)
    self.dynamic_plot.setEnabled(False)
    # start update figure every 1s; flag "update_on" : 1 is on and 0 is Off
    self._update_on = 1
    self._timer.timeout.connect(self.update_fig)
    self._timer.start(1000) # plot after 1s delay

  def update_fig(self):
    self._t+=1
    print(self._t)
    self._delay_t.append(self._t)
    print(self._delay_t)
    #new_counts=random.randint(100,900)
    new_counts= 2 * self._t - self._t * np.cos(self._t / 2 / np.pi * 1000)
    self._counts.append(new_counts)
    print(self._counts)
    self.fig2.axes.cla()
    self.fig2.axes.plot(self._delay_t,self._counts,'-ob')
    self.fig2.axes.set_title("signals",fontsize=18,color='c')
    self.fig2.axes.set_xlabel("delay(s)",fontsize=18,color='c')
    self.fig2.axes.set_ylabel("counts",fontsize=18,color='c')
    self.fig2.draw()

改进说明

后续可以通过引入多线程,单独进行数据采集、显示和保存,完善功能。

最终完整代码

# -*- coding: utf-8 -*-

"""
Module: plot data realtime.
Created on 2020/07/12 by Blog Author VERtiCaL at SSRF
"""

import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication,QMainWindow,QGridLayout
from PyQt5.QtCore import QTimer,pyqtSlot,QThread
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import sys,random, time,os,re
from Ui_Realtimer_Plot import Ui_MainWindow


# class Myplot for plotting with matplotlib
class Myplot(FigureCanvas):
  def __init__(self, parent=None, width=5, height=3, dpi=100):
    # normalized for 中文显示和负号
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False

    # new figure
    self.fig = Figure(figsize=(width, height), dpi=dpi)
    # activate figure window
    # super(Plot_dynamic,self).__init__(self.fig)
    FigureCanvas.__init__(self, self.fig)
    self.setParent(parent)
    #self.fig.canvas.mpl_connect('button_press_event', self)
    # sub plot by self.axes
    self.axes= self.fig.add_subplot(111)
    # initial figure
    self.compute_initial_figure()

    # size policy
    FigureCanvas.setSizePolicy(self,
                  QtWidgets.QSizePolicy.Expanding,
                  QtWidgets.QSizePolicy.Expanding)
    FigureCanvas.updateGeometry(self)

  def compute_initial_figure(self):
    pass



# class for plotting a specific figure static or dynamic
class static_fig(Myplot):
  def __init__(self,*args,**kwargs):
    Myplot.__init__(self,*args,**kwargs)

  def compute_initial_figure(self):
    x=np.linspace(0,2*np.pi,100)
    y=x*np.sin(x)
    self.axes.plot(x,y)
    self.axes.set_title("signals")
    self.axes.set_xlabel("delay(s)")
    self.axes.set_ylabel("counts")


class dynamic_fig(Myplot):
  def __init__(self,*args,**kwargs):
    Myplot.__init__(self,*args,**kwargs)

  def compute_initial_figure(self):
    counts = [1,10]
    delay_t = [0,1]
    self.axes.plot(delay_t,counts,'-ob')
    self.axes.set_title("signals")
    self.axes.set_xlabel("delay(s)")
    self.axes.set_ylabel("counts")



# class for the application window
class AppWindow(QMainWindow,Ui_MainWindow):
  def __init__(self,parent=None):
    super(AppWindow,self).__init__(parent)
    self.setupUi(self)
    # ^O^ static_fig can changed to any other function
    #self.fig1=static_fig(width=5, height=4, dpi=100)
    self.fig1 = static_fig(width=5, height=3, dpi=72)
    self.fig2 = dynamic_fig(width=5, height=3, dpi=72)
    # add NavigationToolbar in the figure (widgets)
    self.fig_ntb1 = NavigationToolbar(self.fig1, self)
    self.fig_ntb2 = NavigationToolbar(self.fig2, self)
    #self.Start_plot.clicked.connect(self.plot_cos)
    # add the static_fig in the Plot box
    self.gridlayout1=QGridLayout(self.Plot_static)
    self.gridlayout1.addWidget(self.fig1)
    self.gridlayout1.addWidget(self.fig_ntb1)
    # add the dynamic_fig in the Plot box
    self.gridlayout2 = QGridLayout(self.Plot_dynamic)
    self.gridlayout2.addWidget(self.fig2)
    self.gridlayout2.addWidget(self.fig_ntb2)
    # initialized flags for static/dynamic plot: on is 1,off is 0
    self._timer = QTimer(self)
    self._t = 1
    self._counts = []
    self._delay_t = []
    self._Static_on=0
    self._update_on=0



  @pyqtSlot()
  def on_Static_plot_clicked(self):
    self.plot_cos()
    self._Static_on=1
    #self.Start_plot.setEnabled(False)

  global nc
  nc=1
  def plot_cos(self):
    #print('nc=%d\n' %self.nc)
    global nc
    nc+=1
    self.fig1.axes.cla()
    self.t=np.arange(0,15,0.1)
    self.y=2*nc*self.t-self.t*np.cos(self.t/2/np.pi*1000)
    self.fig1.axes.plot(self.t,self.y)
    self.fig1.axes.set_title("signals",fontsize=18,color='c')
    self.fig1.axes.set_xlabel("delay(s)",fontsize=18,color='c')
    self.fig1.axes.set_ylabel("counts",fontsize=18,color='c')
    self.fig1.draw()

  @pyqtSlot()
  def on_dynamic_plot_clicked(self):
    print('start dynamic ploting')
    self.Static_plot.setEnabled(False)
    self.dynamic_plot.setEnabled(False)
    # start update figure every 1s; flag "update_on" : 1 is on and 0 is Off
    self._update_on = 1
    self._timer.timeout.connect(self.update_fig)
    self._timer.start(1000) # plot after 1s delay

  
  
  def update_fig(self):
    self._t+=1
    print(self._t)
    self._delay_t.append(self._t)
    print(self._delay_t)
    #new_counts=random.randint(100,900)
    new_counts= 2 * self._t - self._t * np.cos(self._t / 2 / np.pi * 1000)
    self._counts.append(new_counts)
    print(self._counts)
    self.fig2.axes.cla()
    self.fig2.axes.plot(self._delay_t,self._counts,'-ob')
    self.fig2.axes.set_title("signals",fontsize=18,color='c')
    self.fig2.axes.set_xlabel("delay(s)",fontsize=18,color='c')
    self.fig2.axes.set_ylabel("counts",fontsize=18,color='c')
    self.fig2.draw()

  @pyqtSlot()
  def on_End_plot_clicked(self):
    if self._update_on==1:
      self._update_on=0
      self._timer.timeout.disconnect(self.update_fig)
      self.dynamic_plot.setEnabled(True)
    else:
      pass

  @pyqtSlot()
  def on_Erase_plot_clicked(self):
    self.fig1.axes.cla()
    self.fig1.draw()
    self.fig2.axes.cla()
    self.fig2.draw()
    if self._update_on==1:
      self._update_on=0
      self._delay_t=[]
      self._counts=[]
      self.fig2.axes.cla()
      self.fig2.draw()
      self._timer.timeout.disconnect(self.update_fig)
      self.dynamic_plot.setEnabled(True)
    else:
      pass
    self.Static_plot.setEnabled(True)
    #self.Erase_plot.setEnabled(False)
  

if __name__=="__main__":
  app = QApplication(sys.argv)
  win = AppWindow()
  win.show()
  sys.exit(app.exec_())

到此这篇关于利用PyQt5+Matplotlib 绘制静态/动态图的实现代码的文章就介绍到这了,更多相关PyQt5+Matplotlib静态/动态图内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python tornado队列示例-一个并发web爬虫代码分享
Jan 09 Python
Python面向对象之继承和组合用法实例分析
Aug 27 Python
对python打乱数据集中X,y标签对的方法详解
Dec 14 Python
python实现弹跳小球
May 13 Python
Django模型修改及数据迁移实现解析
Aug 01 Python
python中单下划线(_)和双下划线(__)的特殊用法
Aug 29 Python
wxPython修改文本框颜色过程解析
Feb 14 Python
python实现扫雷游戏
Mar 03 Python
python3.6环境下安装freetype库和基本使用方法(推荐)
May 10 Python
python 图像插值 最近邻、双线性、双三次实例
Jul 05 Python
详解Pycharm安装及Django安装配置指南
Sep 15 Python
python 使用xlsxwriter循环向excel中插入数据和图片的操作
Jan 01 Python
Python使用itcaht库实现微信自动收发消息功能
Jul 13 #Python
解决Pycharm 中遇到Unresolved reference 'sklearn'的问题
Jul 13 #Python
解决Python中导入自己写的类,被划红线,但不影响执行的问题
Jul 13 #Python
浅析Python 抽象工厂模式的优缺点
Jul 13 #Python
python正则表达式的懒惰匹配和贪婪匹配说明
Jul 13 #Python
浅析Python 简单工厂模式和工厂方法模式的优缺点
Jul 13 #Python
对python中list的五种查找方法说明
Jul 13 #Python
You might like
php实现无限级分类实现代码(递归方法)
2011/01/01 PHP
YII中assets的使用示例
2014/07/31 PHP
php使用APC实现实时上传进度条功能
2015/10/26 PHP
跨浏览器开发经验总结(三)   警惕“IE依赖综合症”
2010/05/13 Javascript
在JavaScript中获取请求的URL参数[正则]
2010/12/25 Javascript
js实例属性和原型属性示例详解
2014/11/23 Javascript
了不起的node.js读书笔记之例程分析
2014/12/22 Javascript
javascript js 操作数组 增删改查的简单实现
2016/06/20 Javascript
JavaScript异步加载问题总结
2018/02/17 Javascript
webpack常用配置总览(小结)
2019/11/18 Javascript
Vue 打包的静态文件不能直接运行的原因及解决办法
2020/11/19 Vue.js
[57:24]LGD vs VGJ.T 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Python实现比较两个列表(list)范围
2015/06/12 Python
深入理解python中函数传递参数是值传递还是引用传递
2017/11/07 Python
python 对象和json互相转换方法
2018/03/22 Python
用TensorFlow实现多类支持向量机的示例代码
2018/04/28 Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
2019/06/27 Python
Pycharm使用远程linux服务器conda/python环境在本地运行的方法(图解))
2019/12/09 Python
Django集成celery发送异步邮件实例
2019/12/17 Python
Python综合应用名片管理系统案例详解
2020/01/03 Python
CSS3绘制不规则图形的一些方法示例
2015/11/07 HTML / CSS
使用css创建三角形 使用CSS3创建3d四面体原理及代码(html5实践)
2013/01/06 HTML / CSS
德国鞋子网上商店:Omoda.de
2017/03/31 全球购物
墨尔本最受欢迎的复古风格品牌:Princess Highway
2018/12/21 全球购物
Farfetch台湾官网:奢侈品牌时尚购物平台
2019/06/17 全球购物
奥地利购买珠宝和手表网站:ELLA JUWELEN
2019/09/03 全球购物
请写出 float x 与"零值"比较的 if 语句
2016/01/04 面试题
Java文件和目录(IO)操作
2014/08/26 面试题
大学本科生的个人自我评价
2013/12/09 职场文书
普罗米修斯教学反思
2014/02/06 职场文书
大学班级干部的自我评价分享
2014/02/10 职场文书
村委会换届选举方案
2014/05/03 职场文书
起诉状范本
2015/05/20 职场文书
市级三好生竞选稿
2015/11/21 职场文书
学生早退检讨书(范文)
2019/08/19 职场文书
Python实现信息轰炸工具(再也不怕说不过别人了)
2021/06/11 Python