Pyqt5界面显示matplotlib图形

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

class MyFigure(FigureCanvas):
    def __init__(self,width=5, height=4, dpi=100):
        #第一步:创建一个创建Figure
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        #第二步:在父类中激活Figure窗口
        super().__init__(self.fig)
        self.axes = self.fig.add_subplot(111)

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QLineEdit的学习")
        self.resize(500, 500)
        self.setup_ui()

    def setup_ui(self):
        self.QPushButten按钮()
        self.QGridLayout布局()


    def QPushButten按钮(self):
        # self.btn = QPushButton(self)
        # self.btn.setText("按钮")
        self.btn1 = QPushButton(self)
        self.btn1.setText("按钮1")

        self.btn2 = QPushButton(self)
        self.btn2.setText("按钮2")
        # self.btn3 = QPushButton(self)
        # self.btn3.setText("按钮3")

 
    def QGridLayout布局(self):
        F = MyFigure(width=3, height=2, dpi=100)
        F1 = MyFigure(width=3, height=2, dpi=100)

        t = np.arange(0.0, 5.0, 0.01)
        s = np.cos(2 * np.pi * t)
        y = np.sin(2 * np.pi * t)
        F.axes.plot(t, s)
        F.fig.suptitle("cos")
        F1.axes.plot(t, y)
        F1.fig.suptitle("sin")

        self.gridlayout = QGridLayout(self)  # 继承容器groupBox
        self.gridlayout.addWidget(F1, 0, 0)
        self.gridlayout.addWidget(self.btn1, 0, 1)
        self.gridlayout.addWidget(self.btn2, 1, 0)
        self.gridlayout.addWidget(F, 1, 1)



if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())

你可能感兴趣的:(qt,python,开发语言)