我无法使用 MatplotLib 生成图表以在 PyQt5 App 上绘图
Posted
技术标签:
【中文标题】我无法使用 MatplotLib 生成图表以在 PyQt5 App 上绘图【英文标题】:I am not able to generate a graph using MatplotLib for plotting on PyQt5 App 【发布时间】:2018-10-11 14:07:27 【问题描述】:我想知道 Matplotlib 绘图在 PyQT5 桌面应用程序上的工作原理。 我尝试了几种方法来绘制图表,但没有帮助。 我使用了打印功能,以便我至少可以知道该图已绘制。它按绘图打印,但不显示图形。在这里需要一些帮助。以下是我的代码
'''
Created on Oct 8, 2018
@author: kjohn
'''
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QMessageBox, QMainWindow, QVBoxLayout
from PyQt5.QtGui import QIcon
import os
import csv
import matplotlib
from matplotlib import figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
class MatplotlibFigure(QWidget):
def __init__(self):
super().__init__()
self.figure = matplotlib.figure.Figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
def plot(self):
self.figure.clf()
ax = self.figure.add_subplot(111)
x = [i for i in range(100)]
y = [i**0.5 for i in x]
ax.plot(x, y, 'g*-')
self.canvas.draw_idle()
print('PLOTTED')
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1001, 651)
font = QtGui.QFont()
font.setFamily("Segoe UI")
Form.setFont(font)
Form.setMouseTracking(True)
Form.setWindowTitle("Data Analytical Tool Kit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(440, 70, 121, 25))
self.pushButton.clicked.connect(self.inputfileselector)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setMouseTracking(True)
self.pushButton.setObjectName("pushButton")
self.comboBox = QtWidgets.QComboBox(Form)
self.comboBox.setGeometry(QtCore.QRect(670, 70, 241, 30))
font = QtGui.QFont()
font.setFamily("Segoe UI")
self.comboBox.setFont(font)
self.comboBox.setMouseTracking(True)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem('Site Classifier')
self.comboBox.addItem('Account Classifier')
self.comboBox.addItem('Meter Classifier')
self.comboBox.addItem('EMS - Audit Engine')
self.comboBox.addItem('Budget Forecaster')
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 70, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(True)
self.lineEdit.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setReadOnly(True)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(21, 51, 91, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(671, 52, 108, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(669, 161, 121, 31))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.plot_data)
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(792, 161, 121, 31))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(440, 117, 121, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setMouseTracking(True)
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.clicked.connect(self.outputfolderselector)
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(20, 117, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setAutoFillBackground(True)
self.lineEdit_2.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(21, 97, 121, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(20, 210, 411, 421))
self.textEdit.setObjectName("textEdit")
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(670, 116, 275, 31))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(440, 166, 121, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_5.setFont(font)
self.pushButton_5.setMouseTracking(True)
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_5.clicked.connect(self.resultfolderselector)
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(20, 166, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(7)
font.setPointSize(10)
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setFont(font)
self.lineEdit_3.setAutoFillBackground(True)
self.lineEdit_3.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit_3.setText("")
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(22, 146, 121, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.figure = matplotlib.figure.Figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, Form)
self.verticalLayoutWidget = QtWidgets.QWidget(Form)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(440, 210, 541, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayout.addWidget(self.canvas)
self.verticalLayout.addWidget(self.toolbar)
self.figure = MatplotlibFigure()
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Browse File"))
self.label.setText(_translate("Form", "Input File Upload"))
self.label_2.setText(_translate("Form", "Select Classifier Type"))
self.pushButton_2.setText(_translate("Form", "Submit"))
self.pushButton_3.setText(_translate("Form", "Cancel"))
self.pushButton_4.setText(_translate("Form", "Select Folder"))
self.label_3.setText(_translate("Form", "Output Folder for Logs"))
self.pushButton_5.setText(_translate("Form", "Select Folder"))
self.label_4.setText(_translate("Form", "Result Folder"))
Form.setWindowIcon(QtGui.QIcon('icon.png'))
Form.setWindowTitle('Data Analysis Tool Kit')
Form.setGeometry(180,50,1001,651)
Form.setFixedSize(1001,651)
Form.setWindowFlags( QtCore.Qt.CustomizeWindowHint | QtCore.Qt.MSWindowsFixedSizeDialogHint |QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint )
def inputfileselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
filename = QtWidgets.QFileDialog.getOpenFileName(None, 'Select the Input File', path,'All Files(*.csv*)')
if filename is filename:
self.lineEdit.setText(str(filename))
else:
self.lineEdit.setText("No file was selected.")
QtWidgets.QMessageBox.critical(Form, "Alert", "No file was selected.", QtWidgets.QMessageBox.Close)
def outputfolderselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
startingDir = path
directory = QtWidgets.QFileDialog.getExistingDirectory(None, 'Open working directory', startingDir, QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
self.lineEdit_2.setText(directory)
else:
self.lineEdit_2.setText('')
QtWidgets.QMessageBox.critical(Form, "Alert", "No folder selected.", QtWidgets.QMessageBox.Close)
def resultfolderselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
startingDir = path
directory = QtWidgets.QFileDialog.getExistingDirectory(None, 'Open working directory', startingDir, QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
self.lineEdit_3.setText(directory)
else:
self.lineEdit_3.setText('')
QtWidgets.QMessageBox.critical(Form, "Alert", "No folder selected.", QtWidgets.QMessageBox.Close)
def plot_data(self):
self.figure.plot()
self.canvas.setParent(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
【问题讨论】:
你能创建一个minimal reproducible example吗? (我认为这与您使用“Segoe UI”作为字体的问题无关,一个按钮就足够了,等等......) (我认为这与您使用“Segoe UI”作为字体的问题无关,一个按钮就足够了,等等......)“Segoe UI”只是我的字体用过的。我已经使用 PyQt Designer 创建了应用程序布局并稍微增强了我的代码,以便我可以使用我无法使用的 MatPlotLib 在应用程序上进行绘图。 【参考方案1】:首先你为matlabplot创建了一个class MatplotlibFigure
,同时你在class Ui_Form
中定义了matlab函数,这是一个错误。解决你的问题,
我们需要在class MatplotlibFigure
中定义两个函数,第一个函数initializewidget()
定义了matlabplot 的画布小部件和图形框架,QGridLayout
放置和排列它们。然后第二个函数plot()
绘制输入数据。
def initializewidget(self):
gridlayout = QGridLayout()
self.setLayout(gridlayout)
self.figure = plt.figure(figsize=(5,5))
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas,self)
gridlayout.addWidget(self.canvas,1,0,1,2)
gridlayout.addWidget(self.toolbar,0,0,1,2)
最后应该将 Matlabplot 小部件添加到主小部件布局中。
self.matplot = MatplotlibFigure()
self.verticalLayout.addWidget(self.matplot)
Matplotlib.pyplot
用于获得更好的图表,甚至可以自定义不同样式的图表。
import matplotlib.pyplot as plt
plt.style.use('ggplot')
当前工作画面
工作代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit,
QFileDialog, QMessageBox, QMainWindow, QVBoxLayout, QGridLayout
from PyQt5.QtGui import QIcon
import os
import csv
import matplotlib
from matplotlib import figure
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
class MatplotlibFigure(QWidget):
def __init__(self):
super().__init__()
self.initializewidget()
self.plot()
def initializewidget(self):
gridlayout = QGridLayout()
self.setLayout(gridlayout)
self.figure = plt.figure(figsize=(5,5))
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas,self)
gridlayout.addWidget(self.canvas,1,0,1,2)
gridlayout.addWidget(self.toolbar,0,0,1,2)
def plot(self):
ax = self.figure.add_subplot(111)
x = [i for i in range(100)]
y = [1*i for i in x]
ax.plot(x, y, 'g*-')
self.canvas.draw()
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1001, 651)
font = QtGui.QFont()
font.setFamily("Segoe UI")
Form.setFont(font)
Form.setMouseTracking(True)
Form.setWindowTitle("Data Analytical Tool Kit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(440, 70, 121, 25))
self.pushButton.clicked.connect(self.inputfileselector)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setMouseTracking(True)
self.pushButton.setObjectName("pushButton")
self.comboBox = QtWidgets.QComboBox(Form)
self.comboBox.setGeometry(QtCore.QRect(670, 70, 241, 30))
font = QtGui.QFont()
font.setFamily("Segoe UI")
self.comboBox.setFont(font)
self.comboBox.setMouseTracking(True)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem('Site Classifier')
self.comboBox.addItem('Account Classifier')
self.comboBox.addItem('Meter Classifier')
self.comboBox.addItem('EMS - Audit Engine')
self.comboBox.addItem('Budget Forecaster')
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 70, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(True)
self.lineEdit.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setReadOnly(True)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(21, 51, 91, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(671, 52, 108, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(669, 161, 121, 31))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.plot_data)
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(792, 161, 121, 31))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(440, 117, 121, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setMouseTracking(True)
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.clicked.connect(self.outputfolderselector)
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(20, 117, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setAutoFillBackground(True)
self.lineEdit_2.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(21, 97, 121, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(20, 210, 411, 421))
self.textEdit.setObjectName("textEdit")
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(670, 116, 275, 31))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(440, 166, 121, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.pushButton_5.setFont(font)
self.pushButton_5.setMouseTracking(True)
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_5.clicked.connect(self.resultfolderselector)
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(20, 166, 411, 25))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(7)
font.setPointSize(10)
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setFont(font)
self.lineEdit_3.setAutoFillBackground(True)
self.lineEdit_3.setInputMethodHints(QtCore.Qt.ImhHiddenText)
self.lineEdit_3.setText("")
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(22, 146, 121, 16))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.matplot = MatplotlibFigure()
self.verticalLayoutWidget = QtWidgets.QWidget(Form)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(440, 210, 541, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
# self.verticalLayout.addWidget(self.matplot) # comment this line
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Browse File"))
self.label.setText(_translate("Form", "Input File Upload"))
self.label_2.setText(_translate("Form", "Select Classifier Type"))
self.pushButton_2.setText(_translate("Form", "Submit"))
self.pushButton_3.setText(_translate("Form", "Cancel"))
self.pushButton_4.setText(_translate("Form", "Select Folder"))
self.label_3.setText(_translate("Form", "Output Folder for Logs"))
self.pushButton_5.setText(_translate("Form", "Select Folder"))
self.label_4.setText(_translate("Form", "Result Folder"))
Form.setWindowIcon(QtGui.QIcon('icon.png'))
Form.setWindowTitle('Data Analysis Tool Kit')
Form.setGeometry(180,50,1001,651)
Form.setFixedSize(1001,651)
Form.setWindowFlags( QtCore.Qt.CustomizeWindowHint |
QtCore.Qt.MSWindowsFixedSizeDialogHint |QtCore.Qt.WindowCloseButtonHint |
QtCore.Qt.WindowMinimizeButtonHint )
def inputfileselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
filename = QtWidgets.QFileDialog.getOpenFileName(None, 'Select the Input
File', path,'All Files(*.csv*)')
if filename is filename:
self.lineEdit.setText(str(filename))
else:
self.lineEdit.setText("No file was selected.")
QtWidgets.QMessageBox.critical(Form, "Alert", "No file was selected.",
QtWidgets.QMessageBox.Close)
def outputfolderselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
startingDir = path
directory = QtWidgets.QFileDialog.getExistingDirectory(None, 'Open working
directory', startingDir, QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
self.lineEdit_2.setText(directory)
else:
self.lineEdit_2.setText('')
QtWidgets.QMessageBox.critical(Form, "Alert", "No folder selected.",
QtWidgets.QMessageBox.Close)
def resultfolderselector(self):
path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
startingDir = path
directory = QtWidgets.QFileDialog.getExistingDirectory(None, 'Open working
directory', startingDir, QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
self.lineEdit_3.setText(directory)
else:
self.lineEdit_3.setText('')
QtWidgets.QMessageBox.critical(Form, "Alert", "No folder selected.",
QtWidgets.QMessageBox.Close)
def plot_data(self):
# self.figure.plot() # comment this line
# self.canvas.setParent(self) # comment this line
self.verticalLayout.addWidget(self.matplot) # <--- Add this line
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
【讨论】:
当我运行应用程序时会生成图表上的图表,它不应该那样做,它应该在我点击“pushbutton_2”时显示图表,这是“提交”按钮“ui_form”....我已经编写了一个 self.connect 到“提交”按钮,但它抛出一个错误为“AttributeError:'Ui_Form' 对象没有属性'matplot'”可能是我在这里又错过了一些东西。 @JohnK。解决方案是,在函数plot_data()
下擦除self.figure.plot()
和self.canvas.setParent(self)
,并添加此代码行self.verticalLayout.addWidget(self.matplot)
。最终代码添加完毕。
@JohnK,如果我的回答对您有帮助,请不要忘记将其标记为正确。
非常感谢。工作完美。如果我想在每次点击提交按钮时清除绘图并重新生成它怎么办?
你可以发帖问另一个问题。提供您的代码并准确解释您想要做什么?我认为您必须使用插槽和信号方法。以上是关于我无法使用 MatplotLib 生成图表以在 PyQt5 App 上绘图的主要内容,如果未能解决你的问题,请参考以下文章
无法编辑由 Matplotlib 导出并在 Illustrator 中打开的图表中的文本