PyQt:Python图形化界面编程 PyQt5中嵌入matplotlib

目录

 

前言:

1.  设计界面

2. 引入matplotlib

3. 完整代码


前言:

前面已经讲解过如何通过QT-desinger+PYUIC设计交互界面,已经能够设置界面以及添加交互事件了。

传送门:https://blog.csdn.net/weixin_41137248/article/details/88874420

但是在最近编程过程中又遇到了一个问题,我想要绘图,在PYQT设计的界面中绘图,例如直方图,灰度图等。

想了很久,找了很多资料,最后找到了解决办法,在PyQt5中嵌入matplotlib,最后实现的结果就像这样:

PyQt:Python图形化界面编程 PyQt5中嵌入matplotlib_第1张图片

上面的程序比较复杂,有需要的可以留言,这篇文章用一个小demo来讲解怎么在Pyqt5中嵌入matplotlib。

1.  设计界面

设计界面用QT-designer,前面的文章已经讲解过了,此处不再赘述,我们在我们的布局文件中加入一个GroupBox,调整大小,保存并转化为py文件,添加main函数,如下:

PyQt:Python图形化界面编程 PyQt5中嵌入matplotlib_第2张图片

代码:

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

# Form implementation generated from reading ui file 'matplotlib.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_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(39, 29, 731, 421))
        self.groupBox.setObjectName("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.setTitle(_translate("MainWindow", "GroupBox"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    QMainWindow = QtWidgets.QMainWindow()

    ui = Ui_MainWindow()

    ui.setupUi(QMainWindow)

    QMainWindow.show()

    sys.exit(app.exec_())

2. 引入matplotlib

我们新建一个类继承自matplotlib.backends.backend_qt5agg(这是在PYQt中引入matplotlib的关键)

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

class MyFigure(FigureCanvas):
    def __init__(self, width=5, height=4, dpi=100):
        # 第一步:创建一个创建Figure
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        # 第二步:在父类中激活Figure窗口
        super(MyFigure, self).__init__(self.fig)  # 此句必不可少,否则不能显示图形

画一个简单的cos图形:

 def drawCos(self):
        F = MyFigure(3, 3, 100)
        axes = F.fig.add_subplot(111)
        t = np.arange(0.0, 5.0, 0.01)
        s = np.cos(2 * np.pi * t)
        axes.plot(t, s)
        F.fig.suptitle("cos")
        QtWidgets.QGridLayout(self.groupBox).addWidget(F)

最后的结果:

PyQt:Python图形化界面编程 PyQt5中嵌入matplotlib_第3张图片

3. 完整代码

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

# Form implementation generated from reading ui file 'matplotlib.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
import matplotlib
matplotlib.use("Qt5Agg")  # 声明使用QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np

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.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(39, 29, 731, 421))
        self.groupBox.setObjectName("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.setTitle(_translate("MainWindow", "GroupBox"))


    def drawCos(self):
        F = MyFigure(3, 3, 100)
        axes = F.fig.add_subplot(111)
        t = np.arange(0.0, 5.0, 0.01)
        s = np.cos(2 * np.pi * t)
        axes.plot(t, s)
        F.fig.suptitle("cos")
        QtWidgets.QGridLayout(self.groupBox).addWidget(F)

class MyFigure(FigureCanvas):
    def __init__(self, width=5, height=4, dpi=100):
        # 第一步:创建一个创建Figure
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        # 第二步:在父类中激活Figure窗口
        super(MyFigure, self).__init__(self.fig)  # 此句必不可少,否则不能显示图形


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    QMainWindow = QtWidgets.QMainWindow()

    ui = Ui_MainWindow()

    ui.setupUi(QMainWindow)

    ui.drawCos()

    QMainWindow.show()

    sys.exit(app.exec_())

 

你可能感兴趣的:(Python)