如何将matplot lib与pyqt5集成?我已经尝试了很多集成,但是无法做到。基本上,我想在相同的主窗口中单击一个按钮时显示我的图。
self.pushButton_2.clicked.connect(self.plot)
下面是整个代码,是QT设计器生成的部分代码。
使用此代码,我应该能够跳到绘图功能
def plot(self):
plt.magnitude_spectrum(self.y_time, self.sr)
plt.show()
得到频谱图。
使用pyqt5从Qt设计器生成的示例代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import matplotlib.pyplot as plt
class Ui_MainWindow(object):
def plot():
plt.magnitude_spectrum(rate and data)
plt.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(160, 190, 85, 27))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(40, 110, 85, 27))
self.pushButton_2.clicked.connect(self.plot)
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 27))
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.pushButton.setText(_translate("MainWindow", "display plot "))
self.pushButton_2.setText(_translate("MainWindow", "load audio "))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())