将 Matplotlib 图形嵌入到小部件中 - PyQt5
Posted
技术标签:
【中文标题】将 Matplotlib 图形嵌入到小部件中 - PyQt5【英文标题】:Embed a Matplotlib graphic into a widget - PyQt5 【发布时间】:2017-09-24 14:53:52 【问题描述】:我想将来自 Matplotlib
的图形嵌入到现有 GUI 中,其中包含从 QtDesigner
和 PyQt5
开发的 Widget
和 PushButton
。我可以嵌入图形,但无法调整图形大小以将所有可用/需要的空间放入Widget
。
前端代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(595, 393)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setMaximumSize(QtCore.QSize(100, 16777215))
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setMinimumSize(QtCore.QSize(0, 200))
self.widget.setStyleSheet("background-color: rgb(255, 255, 255);")
self.widget.setObjectName("widget")
self.gridLayout.addWidget(self.widget, 0, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 595, 21))
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", "PushButton"))
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_())
后端代码:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QSizePolicy
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from front_end import Ui_MainWindow
class Graph_init(FigureCanvas):
def __init__(self, parent=None):
fig = Figure()
self.axes = fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class Graph_populate(Graph_init):
def compute_initial_figure(self):
x = [2000,2001,2002,2003,2004]
y = [10,20,30,40,50]
self.axes.plot(x, y)
class GUI(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(GUI, self).__init__(parent)
self.setupUi(self)
self.sc = Graph_populate(self.widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
prog = GUI()
prog.showMaximized()
sys.exit(app.exec_())
谢谢
【问题讨论】:
【参考方案1】:在 PyQt 中,您通常会通过将小部件添加到布局中来将小部件添加到应用程序中。在代码中,您似乎忘记了执行该步骤。
因此,如果 self.gridLayout
是您希望 FigureCanvas
驻留的布局,则需要将后者添加到前者作为
self.gridLayout.addWidget(self.sc, 0, 1, 1, 1)
另一方面,self.widget
的作用在这里并不清楚,您可能会删除它。 self.centralwidget
可以充当父母的角色,如果真的需要的话,
self.sc = Graph_populate(self.centralwidget)
【讨论】:
代码:self.gridLayout.addWidget(self.sc, 0, 1, 1, 1) 已经包含在前端部分,为什么后端无法识别呢? 不,不是,至少我看不到。以上是关于将 Matplotlib 图形嵌入到小部件中 - PyQt5的主要内容,如果未能解决你的问题,请参考以下文章
如何使 FigureCanvas 在嵌入 pyqt GUI 的 matplotlib 小部件中填充整个 Figure?