在Qtdesigner 设计界面中加入matplotlib图形
UI文件
# -*- coding: utf-8 -*-
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QFormLayout, QMainWindow, QMenuBar,
QPushButton, QSizePolicy, QStatusBar, QWidget)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.formLayoutWidget = QWidget(self.centralwidget)
self.formLayoutWidget.setObjectName(u"formLayoutWidget")
self.formLayoutWidget.setGeometry(QRect(50, 30, 381, 201))
self.formLayout = QFormLayout(self.formLayoutWidget)
self.formLayout.setObjectName(u"formLayout")
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.sinbtn = QPushButton(self.centralwidget)
self.sinbtn.setObjectName(u"sinbtn")
self.sinbtn.setGeometry(QRect(110, 260, 80, 25))
self.cosbtn = QPushButton(self.centralwidget)
self.cosbtn.setObjectName(u"cosbtn")
self.cosbtn.setGeometry(QRect(260, 260, 80, 25))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 800, 22))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.sinbtn.setText(QCoreApplication.translate("MainWindow", u"Plot sin", None))
self.cosbtn.setText(QCoreApplication.translate("MainWindow", u"Plot cos", None))
# retranslateUi
主文件
import sys
import matplotlib
matplotlib.use('Qt5Agg')
from PySide6.QtWidgets import QMainWindow, QApplication
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
import numpy as np
import pandas as pd
from plot_ui import Ui_MainWindow
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.__create_connections()
def __create_connections(self):
self.ui.sinbtn.clicked.connect(self.__plotsin)
self.ui.cosbtn.clicked.connect(self.__plotcos)
def __clear_item(self):# 清除图像
item_list = list(range(self.ui.formLayout.count()))
for i in item_list:
item = self.ui.formLayout.itemAt(i)
self.ui.formLayout.removeItem(item)
# if item.widget():
# item.widget().deleteLater()
def __plotsin(self):
self.__clear_item()
sc = MplCanvas(self, dpi=100)
x = np.arange(100)
y = np.sin(x*0.1)
sc.axes.plot(x,y)
self.ui.formLayout.addWidget(sc)
def __plotcos(self):
self.__clear_item()
sc = MplCanvas(self, dpi=100)
x = np.arange(100)
y = np.cos(x*0.1)
sc.axes.plot(x,y,color='r')
self.ui.formLayout.addWidget(sc)
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()