python GUI与matplotlib相结合学习笔记

本文详细介绍了如何使用PyQT5和matplotlib库在GUI中绘制多个图形。通过创建QMainWindow,设置布局,并定义自定义的画布类Mydemo,实现了在四个独立的QGroupBox中分别展示图形的功能。每个图形可根据需要设置横纵坐标轴标题,并能动态绘制数据。此教程适用于需要在Python图形界面中展示数据的开发者。
摘要由CSDN通过智能技术生成

看了很多文章的代码,终于,写了点注释,希望后面用到时回来还能看得懂。# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QSizePolicy, QWidget, QGridLayout, QMainWindow
import sys, time
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1000, 700)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setAlignment(QtCore.Qt.AlignCenter)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setAlignment(QtCore.Qt.AlignCenter)
        self.groupBox_2.setObjectName("groupBox_2")
        self.gridLayout.addWidget(self.groupBox_2, 0, 1, 1, 1)
        self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_3.setAlignment(QtCore.Qt.AlignCenter)
        self.groupBox_3.setObjectName("groupBox_3")
        self.gridLayout.addWidget(self.groupBox_3, 1, 0, 1, 1)
        self.groupBox_4 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_4.setAlignment(QtCore.Qt.AlignCenter)
        self.groupBox_4.setObjectName("groupBox_4")
        self.gridLayout.addWidget(self.groupBox_4, 1, 1, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 713, 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.groupBox.setTitle(_translate("MainWindow", "图1"))
        self.groupBox_2.setTitle(_translate("MainWindow", "图2"))
        self.groupBox_3.setTitle(_translate("MainWindow", "图3"))
        self.groupBox_4.setTitle(_translate("MainWindow", "图4"))

#定义画布
class Mydemo(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=10):
        plt.rcParams['font.family'] = ['SimHei']
        plt.rcParams['axes.unicode_minus'] = False
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        # 设置画布分格子xyz = x*y  第z个分区
        self.axes = self.fig.add_subplot(1, 1, 1)
        # 设置横坐标轴
        self.axes.set_xlim(-10, 10)
        self.axes.set_ylim(-10, 10)

        self.axes.set_ylabel('纵坐标标题')
        self.axes.set_title('标题')

        FigureCanvas.setSizePolicy(self,QSizePolicy.Expanding,QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

class Try(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.data = list(range(10))
        self.huatu()
        self.huatu2()
        self.huatu3()
        self.huatu4()

    def huatu(self):
        # 然后在相应的QGroupBox中添加一个栅格布局
        self.LineFigureLayout = QGridLayout(self.groupBox)
        # 创建画布
        self.figure1 = Mydemo(width=5, height=4, dpi=100)
        self.figure1.axes.set_xlabel('横坐标标题')
        self.LineFigureLayout.addWidget(self.figure1)
        # 然后,将画板添加到布局中
        self.figure1.axes.plot(self.data)

    def huatu2(self):
        # 然后在相应的QGroupBox中添加一个栅格布局
        self.LineFigureLayout = QGridLayout(self.groupBox_2)
        # 创建画布
        self.figure2 = Mydemo(width=5, height=4, dpi=100)
        self.LineFigureLayout.addWidget(self.figure2)
        # 然后,将画板添加到布局中
        self.figure2.axes.plot(self.data)
    def huatu3(self):
        # 然后在相应的QGroupBox中添加一个栅格布局
        self.LineFigureLayout = QGridLayout(self.groupBox_3)
        # 创建画布
        self.figure3 = Mydemo(width=5, height=4, dpi=100)
        self.LineFigureLayout.addWidget(self.figure3)
        # 然后,将画板添加到布局中
        self.figure3.axes.plot(self.data)
    def huatu4(self):
        # 然后在相应的QGroupBox中添加一个栅格布局
        self.LineFigureLayout = QGridLayout(self.groupBox_4)
        # 创建画布
        self.figure4 = Mydemo(width=5, height=4, dpi=100)
        self.LineFigureLayout.addWidget(self.figure4)
        # 然后,将画板添加到布局中
        self.figure4.axes.plot(self.data)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)  # 创建一个QApplication,也就是你要开发的软件app
    ui = Try()  # ui是你创建的ui类的实例化对象
    ui.show()  # 执行QMainWindow的show()方法,显示这个QMainWindow
    sys.exit(app.exec_())  # 使用exit()或者点击关闭按钮退出QApplication

效果图

参考来源:

https://blog.csdn.net/weixin_43008870/article/details/92173542

总结一下PyQT5 + matplotlib的使用方法_u013523007的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hi-那个谁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值