向图中添加导航工具栏(matplotlib 和 PyQt4) [英] Adding Navigation Toolbar to the Figure (matplotlib and PyQt4)

查看:80
本文介绍了向图中添加导航工具栏(matplotlib 和 PyQt4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 PyQt4 GUI 中创建一个导航工具栏,但我无法让它工作,我必须承认我主要是复制粘贴了这段代码,但我并没有真正理解它.如果您能告诉我如何添加NavigationToolbar并说明其制作方法,那就太好了.先感谢您.

I would like to create a navigation Toolbar in my PyQt4 GUI but I can't make it work, I must admit that I mostly copy-pasted this code and I don't really get it. Would be great if you could tell me how to add the NavigationToolbar and explain how it was made. Thank you in advance.

顺便说一句.我看过这篇文章,但我相信它与我的不同,我不能弄清楚如何使用它.

Btw. I have seen This Post but I believe it's different from mine and I can't figure out how to use it.

from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

from matplotlib.figure import Figure

class MplCanvas(FigureCanvas):

    def __init__( self ):
        self.fig = Figure()
        self.ax = self.fig.add_subplot( 111 )

        FigureCanvas.__init__( self, self.fig )
        FigureCanvas.setSizePolicy( self, QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding )
        FigureCanvas.updateGeometry( self )


class matplotlibWidget(QtGui.QWidget):

    def __init__( self, parent = None ):
        QtGui.QWidget.__init__( self, parent )
        self.canvas = MplCanvas()
        #self.toolbar = self.canvas.toolbar #Dunno How :(

        self.vbl = QtGui.QVBoxLayout()
        self.vbl.addWidget( self.canvas )
        self.setLayout( self.vbl )

推荐答案

我设法解决了这个问题,代码应该像这样

I managed to solve the problem, the code should look like this

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

class MplCanvas(FigureCanvas):

    def __init__( self ):
        self.fig = Figure()
        self.ax = self.fig.add_subplot( 111 )

        FigureCanvas.__init__( self, self.fig )
        FigureCanvas.setSizePolicy( self, QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding )
        FigureCanvas.updateGeometry( self )


class matplotlibWidget(QtGui.QWidget):

    def __init__( self, parent = None ):
        QtGui.QWidget.__init__( self, parent )
        self.canvas = MplCanvas() #create canvas that will hold our plot
        self.navi_toolbar = NavigationToolbar(self.canvas, self) #createa navigation toolbar for our plot canvas

        self.vbl = QtGui.QVBoxLayout()
        self.vbl.addWidget( self.canvas )
        self.vbl.addWidget(self.navi_toolbar)
        self.setLayout( self.vbl )

这篇关于向图中添加导航工具栏(matplotlib 和 PyQt4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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