如何在 pyqt 中嵌入 matplotlib - For Dummies

Posted

技术标签:

【中文标题】如何在 pyqt 中嵌入 matplotlib - For Dummies【英文标题】:How to embed matplotlib in pyqt - for Dummies 【发布时间】:2012-09-17 13:04:39 【问题描述】:

我目前正在尝试在我设计的 pyqt4 用户界面中嵌入我想要绘制的图表。因为我对编程几乎是全新的 - 我不明白人们是如何在我找到的示例中进行嵌入的 - this one (at the bottom) 和 that one。

如果有人可以发布一步一步的解释,或者至少是一个非常小、非常简单的代码,那就太棒了。一个 pyqt4 GUI 中的图形和按钮。

【问题讨论】:

【参考方案1】:

其实没那么复杂。相关的 Qt 小部件位于 matplotlib.backends.backend_qt4aggFigureCanvasQTAggNavigationToolbar2QT 通常是您需要的。这些是常规的 Qt 小部件。您将它们视为任何其他小部件。下面是一个非常简单的示例,其中包含 FigureNavigation 和一个绘制一些随机数据的按钮。我已经添加了 cmets 来解释事情。

import sys
from PyQt4 import QtGui

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

import random

class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # a figure instance to plot on
        self.figure = Figure()

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        # Just some button connected to `plot` method
        self.button = QtGui.QPushButton('Plot')
        self.button.clicked.connect(self.plot)

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def plot(self):
        ''' plot some random stuff '''
        # random data
        data = [random.random() for i in range(10)]

        # create an axis
        ax = self.figure.add_subplot(111)

        # discards the old graph
        ax.clear()

        # plot data
        ax.plot(data, '*-')

        # refresh canvas
        self.canvas.draw()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Window()
    main.show()

    sys.exit(app.exec_())

编辑

已更新以反映 cmets 和 API 更改。

NavigationToolbar2QTAgg 更改为 NavigationToolbar2QT 直接导入Figure而不是pyplot 将不推荐使用的ax.hold(False) 替换为ax.clear()

【讨论】:

注意,根据SO note,嵌入时不应该导入pyplot。它做一些时髦的事情,比如用自己的主循环运行自己的 GUI。嵌入时有一个 Matplotlib example 我有一个错误:MatplotlibDeprecationWarning: 这个类在 1.4 中已被弃用,因为它没有超过 NavigationToolbar2QT 的附加功能。请更改您的代码以使用 NavigationToolbar2QT 代替 mplDeprecation) @gs_developer_user3605534 将 NavigationToolbar2QTAgg 替换为 NavigationToolbar2QT 会杀死消息 当我使用这段代码时,它会创建两个窗口,一个 Qt 主窗口和一个 matplotlib 图。我怎样才能避免这种情况,以便它只创建一个 Qt 主窗口?我使用 python 3.4 32bit、matplotlib 1.5.3 和 PyQt4(我还必须将 NavigationToolbar2QTAgg 更改为 NavigationToolbar2QT)。我发现一种解决方案是使用 IPython 我正在使用它在主窗口中放置QVBoxLayout。有没有办法让画布和图形填充一个高矩形(大约是宽的 1.5 倍)?【参考方案2】:

以下是在 PyQt5Matplotlib 2.0 下使用的先前代码的改编。 有一些小的变化:PyQt 子模块的结构,matplotlib 中的其他子模块,已弃用的方法已被替换...


import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

import random

class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # a figure instance to plot on
        self.figure = plt.figure()

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        # Just some button connected to `plot` method
        self.button = QPushButton('Plot')
        self.button.clicked.connect(self.plot)

        # set the layout
        layout = QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def plot(self):
        ''' plot some random stuff '''
        # random data
        data = [random.random() for i in range(10)]

        # instead of ax.hold(False)
        self.figure.clear()

        # create an axis
        ax = self.figure.add_subplot(111)

        # discards the old graph
        # ax.hold(False) # deprecated, see above

        # plot data
        ax.plot(data, '*-')

        # refresh canvas
        self.canvas.draw()

if __name__ == '__main__':
    app = QApplication(sys.argv)

    main = Window()
    main.show()

    sys.exit(app.exec_())

【讨论】:

非常有用的更新,运行良好!您能否为ax.hold 被弃用提供参考?另外,为什么不使用ax.clear 来重用Axes 实例呢? 我认为在将 matplotlib 嵌入 pyqt 时导入 matplotlib.pyplot 不是最佳做法(如果我在这种情况下错了,请纠正我)。如果我是正确的,我在这个 SO 帖子中使用这种方法嵌入了我的 matplotlib,它不导入 pyplot:***.com/questions/43947318/… 如何在画布上绘制图像?【参考方案3】:

对于那些寻找在 PyQt5 中嵌入 Matplotlib 的动态解决方案的人(甚至使用拖放绘制数据)。在 PyQt5 中,您需要在主窗口类上使用 super 来接受 drop。 dropevent函数可以用来获取文件名,剩下的很简单:

def dropEvent(self,e):
        """
        This function will enable the drop file directly on to the 
        main window. The file location will be stored in the self.filename
        """
        if e.mimeData().hasUrls:
            e.setDropAction(QtCore.Qt.CopyAction)
            e.accept()
            for url in e.mimeData().urls():
                if op_sys == 'Darwin':
                    fname = str(NSURL.URLWithString_(str(url.toString())).filePathURL().path())
                else:
                    fname = str(url.toLocalFile())
            self.filename = fname
            print("GOT ADDRESS:",self.filename)
            self.readData()
        else:
            e.ignore() # just like above functions  

对于初学者,参考完整 code 给出以下输出:

【讨论】:

以上是关于如何在 pyqt 中嵌入 matplotlib - For Dummies的主要内容,如果未能解决你的问题,请参考以下文章

如何在 pyqt 中嵌入 matplotlib - For Dummies

如何刷新嵌入在 PYQT4 中的 MatPlotlib?

如何使 FigureCanvas 在嵌入 pyqt GUI 的 matplotlib 小部件中填充整个 Figure?

如何在 PyQt4 小部件中嵌入的 Axes3D (matplotlib) 中启用旋转?

如何在 PyQt 中嵌入 Schemdraw

在 PyQt5 中嵌入 Matplotlib 图形