使用Qt Designer表单和PyQt5在QWidget中绘制matplotlib图

时间:2017-05-12 22:30:26

标签: python matplotlib pyqt5 matplotlib-widget

我不明白将matplotlib图形链接到从Qt Designer创建的表单的最佳方法。我有一个我在QtDesigner中创建的表单,然后通过pyuic5编译为python。我的主要计划是:

import app_framework as af
import matplotlib
from PyQt5 import QtWidgets
import sys

matplotlib.use('Qt5Agg')

app = QtWidgets.QApplication(sys.argv)
form = af.MyApp()
form.show()
app.exec_()

其中myApp调用从Qt Designer创建的app_framework.py表单,然后由pyuic5(design.py)转换:

from PyQt5.QtWidgets import QApplication, QMainWindow
import design

class MyApp(QMainWindow, design.Ui_mainWindow):
   def __init(self):
       super(self.__class__, self).__init__()
       <button initializations>
       <function definitions for button callbacks>

我很困惑在这个框架中我可以将matplotlib图形链接到QtDesigner中的预制空窗口小部件,或类似的东西,这样我就可以在GUI窗口中绘制新数据(文本输入,按钮按等。)

我在SOmatplotlib's site找到了一些线程,但我不确定我是否理解在Qt Designer表单中为此窗口小部件创建空间然后链接绘图的正确过程,和/或创建事后小部件,然后链接和绘图。

到目前为止,我在Qt Creator中创建了一个空的QWidget,然后在pyuic5编译之后,我按如下方式更改了design.py文件:

from PyQt5 import QtCore, QtGui, QtWidgets

# **** ADDED THIS
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
# **** 

class Ui_mainWindow(object):
    def setupUi(self, mainWindow):
        <mainWindow setup stuff>
        self.centralwidget = QtWidgets.QWidget(mainWindow)

        # ****ALTERED THIS FROM self.plotWidget = QtWidgets.QWidget(self.centralWidget)
        self.plotWidget = MplWidget(self.centralWidget)
        # ***** 

        self.plotWidget.setGeometry(QtCore.QRect(20, 250, 821, 591))
        self.plotWidget.setObjectName("plotWidget")

  # **** ADDED THIS 
class MplCanvas(Canvas):
    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        Canvas.__init__(self, self.fig)
        Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        Canvas.updateGeometry(self)


class MplWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.canvas = MplCanvas()
 # ***********

然后app_framework.py为:

from PyQt5.QtWidgets import QApplication, QMainWindow
import design

class MyApp(QMainWindow, design.Ui_mainWindow):
   def __init(self):
       super(self.__class__, self).__init__()
       self.setupUi(self)
       self.pushButton_plotData.clicked.connect(self.plot_data)

    def plot_data(self):
        x=range(0, 10)
        y=range(0, 20, 2)
        self.plotWidget.canvas.ax.plot(x, y)
        self.plotWidget.canvas.draw()

我认为这会有效,但是当我点击情节按钮时,没有任何反应。它没有锁定,它只是没有绘制任何东西。我猜我错过了在这个空小部件中绘制matplotlib图形和/或画布的基本内容。

1 个答案:

答案 0 :(得分:11)

我通过此SO post的帮助找到了解决方案,该链接指向我可能需要购买的一本书的free sample chapter。正如其他一些帖子所提到的那样,人们需要提升&#34;促进&#34;使用标头mplwidget将空白QtWidget写入MplWidget。执行此操作,然后运行pyuic5命令,&#34;从mplwidget导入MplWidget&#34;将出现在design.py文件中,该文件可以随时更新,无需担心重写。然后,使用:

创建一个mplwidget.py文件
# Imports
from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
import matplotlib

# Ensure using PyQt5 backend
matplotlib.use('QT5Agg')

# Matplotlib canvas class to create figure
class MplCanvas(Canvas):
    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        Canvas.__init__(self, self.fig)
        Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        Canvas.updateGeometry(self)

# Matplotlib widget
class MplWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)   # Inherit from QWidget
        self.canvas = MplCanvas()                  # Create canvas object
        self.vbl = QtWidgets.QVBoxLayout()         # Set box for plotting
        self.vbl.addWidget(self.canvas)
        self.setLayout(self.vbl)

然后,app框架可以像我之前一样。运行并按下绘图按钮时,图形将按预期显示。我想我基本上已经完成了所有事情,并且#34;推广&#34;手动QWidget只是错过了vbl的东西,但每次编辑Qt Designer表单时都会被覆盖。

app_framework.py:

from PyQt5.QtWidgets import QApplication, QMainWindow
import design

class MyApp(QMainWindow, design.Ui_mainWindow):
   def __init(self):
       super(self.__class__, self).__init__()
       self.setupUi(self)
       self.pushButton_plotData.clicked.connect(self.plot_data)

    def plot_data(self):
        x=range(0, 10)
        y=range(0, 20, 2)
        self.plotWidget.canvas.ax.plot(x, y)
        self.plotWidget.canvas.draw()

main.py:

from PyQt5 import QtWidgets
import sys

# Local Module Imports
import app_framework as af

# Create GUI application
app = QtWidgets.QApplication(sys.argv)
form = af.MyApp()
form.show()
app.exec_()