matlibplot画图嵌入到PyQt5界面中

matlibplot画的图形会单独弹出一个窗口显示,如果为了减少窗口数量或者美观,也可以将matlibplot图形嵌入到PyQt5开发的窗口界面中。

第一步:程序源码

此文件untitled.py,使用designer.exe简单做了个PyQt5的界面,也可以直接手动写,使用designer.exe为了提高开发效率。

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

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


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(533, 388)
        self.groupBox = QtWidgets.QGroupBox(Dialog)
        self.groupBox.setGeometry(QtCore.QRect(10, 9, 481, 361))
        self.groupBox.setObjectName("groupBox")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.groupBox.setTitle(_translate("Dialog", "GroupBox"))

再建一个主程序文件test.py,代码如下:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from untitled import Ui_Dialog
from PyQt5.QtCore import *

# 导入matplotlib模块并使用Qt5Agg
import matplotlib
matplotlib.use('Qt5Agg')
# 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends中 FigureCanvas继承自QtWidgets.QWidget)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets,QtGui
from PyQt5.QtWidgets import *
import matplotlib.pyplot as plt
import sys
import numpy as np
from numpy import *
import pandas as pd

class Test(QDialog, Ui_Dialog):
    def __init__(self,parent=None):
        super(Test, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowMinMaxButtonsHint | Qt.WindowCloseButtonHint)

        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.gridlayout = QGridLayout(self.groupBox)  # 继承容器groupBox
        self.gridlayout.addWidget(self.canvas,0,1)
        self.plot_()

    def plot_(self):
        ax = self.figure.add_axes([0.1,0.1,0.8,0.8])
        ax.clear() #每次绘制一个函数时清空绘图
        self.x = np.linspace(0, 5*np.pi, 400)
        self.y = np.sin(self.x)
        ax.plot(self.x, self.y)
        self.canvas.draw()

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

第二步:运行程序

在cmd命令行窗口运行程序: python test.py

运行效果如下:

matlibplot画图嵌入到PyQt5界面中_第1张图片

你可能感兴趣的:(python学习)