Matplotlib之PyQt5展示

本文介绍如何利用Matplotlib在PyQt5界面中绘制动态图表,通过qtdesigner设计界面并使用PyUIC编译,实现了一个包含matplotlib图形的GUI应用,展示了从界面设计到逻辑实现的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

Matplotlib图画PyQt5展示

**
qt designer制作界面
在这里插入图片描述

保存文件名为gui.ui文件,保存后用PyUIC编译为gui.py文件
编译后的代码

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

# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(510, 431)
        self.widget = QtWidgets.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(30, 50, 441, 321))
        self.widget.setStyleSheet("background-color: rgb(85, 255, 255);")
        self.widget.setObjectName("widget")
        self.groupBox = QtWidgets.QGroupBox(self.widget)
        self.groupBox.setGeometry(QtCore.QRect(60, 50, 321, 221))
        self.groupBox.setTitle("")
        self.groupBox.setObjectName("groupBox")

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))


新建main.py文件用于编写逻辑文件

#!/ust/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from gui import Ui_Form #导入界面文件

import numpy as np
import matplotlib
matplotlib.use("Qt5Agg")  # 声明使用QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

'''
创建一个matplotlib图形绘制类,通过继承FigureCanvas类,使得该类既是一个PyQt5的Qwidget,又是一个matplotlib的FigureCanvas,这是连接pyqt5与matplotlib的关键
'''
class MyFigure(FigureCanvas):
    def __init__(self,width, height, dpi):
         # 创建一个Figure,该Figure为matplotlib下的Figure,不是matplotlib.pyplot下面的Figure
        self.fig = Figure(figsize=(width, height), dpi=dpi)
         # 在父类中激活Figure窗口,此句必不可少,否则不能显示图形
        super(MyFigure,self).__init__(self.fig)
         # 调用Figure下面的add_subplot方法,类似于matplotlib.pyplot下面的subplot(1,1,1)方法
        self.axes = self.fig.add_subplot(111)

class Mainwindow(QWidget, Ui_Form):
    def __init__(self):
        super(Mainwindow,self).__init__()
        self.setupUi(self)

        self.F = MyFigure(width=3, height=2, dpi=100)
        self.countdot()  #采集需要画的点位
        self.plotcos(self.t,self.s)  #画 图
         # 在GUI的groupBox中创建一个布局,用于添加MyFigure类的实例(即图形)。
        self.gridlayout = QGridLayout(self.groupBox)
        self.gridlayout.addWidget(self.F)

    def countdot(self):
        self.t = np.arange(0.0, 5.0, 0.01)
        self.s = np.cos(2 * np.pi * self.t)
        # self.t = [0,1,2,3,4,5]
        # self.s = [0,1,2,3,4,5]
    def plotcos(self, x, y):
        self.F.axes.plot(x, y)
        self.F.fig.suptitle("cos")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Mainwindow()
    main.show()
    sys.exit(app.exec_())

运行效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

share notes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值