首页 新闻 搜索 专区 学院

请问在python中用matplotlib 画的图,在pyqt5中如何更新图

0
悬赏园豆:160 [已关闭问题] 关闭于 2021-09-14 08:19

如题,我这个是用matplotlib 画的图,在pyqt5的groupBox中更新不了,请问如何更新?哪位大佬能否告知一下

这一段是功能实现板块

 
from PyQt5.Qt import *
import sys
from grobox import Ui_MainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib import pyplot as plt
class Main(QMainWindow,Ui_MainWindow):  # 如果你是用Widget创建的窗口,这里会不同
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.figure4 = plt.figure(facecolor='#F0F0F0', figsize=(2, 4))  # 可选参数,facecolor为背景颜色
        self.canvas4 = FigureCanvas(self.figure4)
        c = QGridLayout(self.groupBox_2)
        c.addWidget(self.canvas4)
        self.Draw4()
    def Draw4(self):
        labels = '良率', '不良率'
        #下面的数据是动态的
        lianglv = 57
        bulianglv = 43
        # 设置饼图数据,也就是饼图面积
        sizes = lianglv, bulianglv
        # 设置饼图颜色
        colors = 'ForestGreen', 'red',
        # 设置第二块离开多少距离
        explode = 0, 0.2
        # shadow=True是设置阴影
        plt.pie(sizes, explode=explode, labels=labels,colors=colors, autopct='%1.1f%%', shadow=True, startangle=30)
        # 设置字体,中文不会出现乱码
        plt.rcParams['font.sans-serif'] = ['SimHei']
        plt.rcParams['axes.unicode_minus'] = False
        self.canvas4.draw()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())

下面是UI代码

from PyQt5 import QtCore, QtGui, QtWidgets
 
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(110, 30, 601, 511))
        self.widget.setObjectName("widget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.groupBox_2 = QtWidgets.QGroupBox(self.widget)
        self.groupBox_2.setObjectName("groupBox_2")
        self.verticalLayout.addWidget(self.groupBox_2)
        self.groupBox = QtWidgets.QGroupBox(self.widget)
        self.groupBox.setObjectName("groupBox")
        self.verticalLayout.addWidget(self.groupBox)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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_2.setTitle(_translate("MainWindow", "GroupBox"))
        self.groupBox.setTitle(_translate("MainWindow", "GroupBox"))
 


泡沫°的主页 泡沫° | 菜鸟二级 | 园豆:202
提问于:2021-09-10 08:23
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册