看了很多文章的代码,终于,写了点注释,希望后面用到时回来还能看得懂。# -*- 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