from PyQt5.Qt import *
from matplotlib.figure import Figure
import matplotlib
matplotlib.use("Qt5Agg")
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):
self.fig = Figure(figsize=(width, height), dpi=dpi)
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.btn1 = QPushButton(self)
self.btn1.setText("按钮1")
self.btn2 = QPushButton(self)
self.btn2.setText("按钮2")
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)
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_())