圆月山庄资源网 Design By www.vgjia.com

参考网上的例子,实现了简单的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_())

参考链接:https://www.cnblogs.com/hhh5460/p/4322652.html

标签:
PyQt5结合matplotlib,PyQt5,matplotlib结合

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。