欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PyQt5结合matplotlib绘图的实现示例

程序员文章站 2022-06-17 20:51:41
参考网上的例子,实现了简单的matplotlib pyqt5绘图相关知识点: (1)pyqt5中添加控件要在布局中添加 (2)布局可以使用replacewidget替换控件 (3)信号与槽机制time...

参考网上的例子,实现了简单的matplotlib pyqt5绘图 

相关知识点:
 (1)pyqt5中添加控件要在布局中添加
 (2)布局可以使用replacewidget替换控件
 (3)信号与槽机制

timer = qtcore.qtimer(self)
timer.timeout.connect(self.update_figure)

self.btnplot.clicked.connect(self.plotbutton_callback)

实现的效果

PyQt5结合matplotlib绘图的实现示例

import sys
from pyqt5 import qtcore, qtgui, uic
from pyqt5.qtwidgets import qmainwindow, qaction, qapp, qapplication, qmessagebox, qvboxlayout, qsizepolicy, qwidget

from pyqt5.qtgui import qicon
from matplotlib.backends.backend_qt5agg import figurecanvasqtagg as figurecanvas
from matplotlib.figure import figure
import matplotlib.pyplot as plt
import numpy as np

qtcreatorfile = "matplotlib_ui.ui"
# 使用uic加载
ui_mainwindow, qtbaseclass = uic.loaduitype(qtcreatorfile)

class mymplcanvas(figurecanvas):
  """这是一个窗口部件,即qwidget(当然也是figurecanvasagg)"""
  def __init__(self, parent=none, width=5, height=4, dpi=100):
    fig = figure(figsize=(width, height), dpi=dpi)
    self.axes = fig.add_subplot(111)
    # 每次plot()调用的时候,我们希望原来的坐标轴被清除(所以false)
    self.axes.hold(false)
    self.axes.grid('on')

    self.compute_initial_figure()

    #
    figurecanvas.__init__(self, fig)
    self.setparent(parent)

    figurecanvas.setsizepolicy(self,
                  qsizepolicy.expanding,
                  qsizepolicy.expanding)
    figurecanvas.updategeometry(self)

  def compute_initial_figure(self):
    pass

class mystaticmplcanvas(mymplcanvas):
  """静态画布:一条正弦线"""
  def compute_initial_figure(self):
    t = np.arange(0.0, 3.0, 0.01)
    s = np.sin(2 * np.pi * t)
    self.axes.grid('on')
    self.axes.plot(t, s)


class mydynamicmplcanvas(mymplcanvas):
  """动态画布:每秒自动更新,更换一条折线。"""
  def __init__(self, *args, **kwargs):
    mymplcanvas.__init__(self, *args, **kwargs)
    timer = qtcore.qtimer(self)
    timer.timeout.connect(self.update_figure)
    timer.start(1000)

  def compute_initial_figure(self):
    self.axes.grid('on')
    self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')

  def update_figure(self):
    # 构建4个随机整数,位于闭区间[0, 10]
    l = [np.random.randint(0, 10) for i in range(4)]
    self.axes.grid('on')
    self.axes.plot([0, 1, 2, 3], l, 'r')
    self.draw()

class myapp(qmainwindow, ui_mainwindow):

  def __init__(self):
    qmainwindow.__init__(self)
    ui_mainwindow.__init__(self)
    super().__init__()

    self.initui()
    self.initbtn()
    self.initframe()

  def initframe(self):
    self.main_widget = self.frame
    self.layout = qvboxlayout(self.main_widget)
    self.f = mymplcanvas(self.main_widget)
    self.layout.addwidget(self.f)

  def initui(self):        

    self.setupui(self)
    self.setwindowtitle("pyqt5结合matplotlib绘图")
    self.setwindowicon(qicon("rocket.ico"))  # 设置图标,linux下只有任务栏会显示图标

    self.show()

  def initbtn(self):
    self.btnplot.clicked.connect(self.plotbutton_callback)
    self.btnplot.settooltip("button")     

  def plotbutton_callback(self):

    self.drawframe()

  def drawframe(self):

    sc = mystaticmplcanvas(self.main_widget, width=5, height=4, dpi=100)
    dc = mydynamicmplcanvas(self.f, width=5, height=4, dpi=100)
    self.layout.addwidget(sc)
    self.layout.replacewidget(self.f,dc) # 替换控件

if __name__ == '__main__':

  app = qapplication(sys.argv)
  ex = myapp()
  sys.exit(app.exec_())

参考链接:

到此这篇关于pyqt5结合matplotlib绘图的实现示例的文章就介绍到这了,更多相关pyqt5结合matplotlib内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: PyQt5 matplotlib