如何在 pyqt 中嵌入 matplotlib - 对于傻瓜 [英] How to embed matplotlib in pyqt - for Dummies

查看:61
本文介绍了如何在 pyqt 中嵌入 matplotlib - 对于傻瓜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将要绘制的图形嵌入到我设计的 pyqt4 用户界面中.因为我对编程几乎完全陌生 - 我不明白人们如何在我发现的示例中嵌入 - 这个(在底部)那个.

I am currently trying to embed a graph I want to plot in a pyqt4 user interface I designed. As I am almost completely new to programming - I do not get how people did the embedding in the examples I found - this one (at the bottom) and that one.

如果有人可以发布一步一步的解释或者至少是一个非常小的、非常简单的代码,只会创建例如一个 pyqt4 GUI 中的图形和按钮.

It would be awesome if anybody could post a step-by-step explanation or at least a very small, very simple code only creating e.g. a graph and a button in one pyqt4 GUI.

推荐答案

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

It is not that complicated actually. Relevant Qt widgets are in matplotlib.backends.backend_qt4agg. FigureCanvasQTAgg and NavigationToolbar2QT are usually what you need. These are regular Qt widgets. You treat them as any other widget. Below is a very simple example with a Figure, Navigation and a single button that draws some random data. I've added comments to explain things.

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_())

编辑:

更新以反映评论和 API 更改.

Updated to reflect comments and API changes.

  • NavigationToolbar2QTAgg 更改为 NavigationToolbar2QT
  • 直接导入Figure而不是pyplot
  • 将不推荐使用的 ax.hold(False) 替换为 ax.clear()
  • NavigationToolbar2QTAgg changed with NavigationToolbar2QT
  • Directly import Figure instead of pyplot
  • Replace deprecated ax.hold(False) with ax.clear()

这篇关于如何在 pyqt 中嵌入 matplotlib - 对于傻瓜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆