4

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 )

1 Answer 1

6

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 )
Sign up to request clarification or add additional context in comments.

1 Comment

I am wondering if there is a way to declare the NavigationToolbar in the canvas class declaration, not in the widget/ui one. Typically my ui files are generated automatically from QtDesigner then I just have to import them in the main program and build the logic around them, so ideally nothing like .addWidget should appear in that program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.