如何将Matplotlib嵌入pyqt-傻瓜 [英] How to embed matplotlib in pyqt - for Dummies

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

问题描述

我目前正在尝试将要绘制的图形嵌入我设计的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.

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

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

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