Someone in office wanted a quick demo on how to use zoom functionality of matplotlib with pyQt. Pointed him to some good and basic matplotlib examples but none was good enough.

Next day morning, just went to net and found this post titled How to embed matplotib in pyqt – for Dummies. And building on the supplied code, I added the needed functionally.

So here’s the code to embed matplotlib with pyQt with the ability to zoom, pan the data.


"""
Created on Thu Dec 12 08:38:21 2013

@author: Sukhbinder Singh

Simple QTpy and MatplotLib example with Zoom/Pan

Built on the example provided at
How to embed matplotib in pyqt - for Dummies
http://stackoverflow.com/questions/12459811/how-to-embed-matplotib-in-pyqt-for-dummies

"""
import sys
from PyQt4 import QtGui

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt

import random

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

        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)

        
        self.toolbar = NavigationToolbar(self.canvas, self)
        self.toolbar.hide()

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

        self.button1 = QtGui.QPushButton('Zoom')
        self.button1.clicked.connect(self.zoom)
        
        self.button2 = QtGui.QPushButton('Pan')
        self.button2.clicked.connect(self.pan)
        
        self.button3 = QtGui.QPushButton('Home')
        self.button3.clicked.connect(self.home)


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

    def home(self):
        self.toolbar.home()
    def zoom(self):
        self.toolbar.zoom()
    def pan(self):
        self.toolbar.pan()
        
    def plot(self):
        ''' plot some random stuff '''
        data = [random.random() for i in range(25)]
        ax = self.figure.add_subplot(111)
        ax.hold(False)
        ax.plot(data, '*-')
        self.canvas.draw()

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

    main = Window()
    main.setWindowTitle('Simple QTpy and MatplotLib example with Zoom/Pan')
    main.show()

    sys.exit(app.exec_())

I don’t particularly like Qt. But I guess the exercise was worth it as it sparked a series of other experiments.

11 responses to “Simple pyQT and MatplotLib Example with Zoom/Pan”

  1. […] this post said, I don’t enjoy coding with pyQt, but somehow wxPython escapes that […]

    Like

  2. Excellent example. Exactly what i searched for.

    Like

  3. […] have already done this with wx and with pyqt and we know tkinter does not suck, so here’s simple demo of using matplotlib with […]

    Like

  4. Thanks so much for this — of real benefit! If anyone is looking to convert this to PySide instead of pyQt, the only thing that changes are the import statements. The PySide alternative import looks like this:

    import sys

    from PySide import QtGui
    from PySide import QtCore

    import matplotlib
    matplotlib.use(‘Qt4Agg’)
    matplotlib.rcParams[‘backend.qt4′]=’PySide’

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

    import matplotlib.pyplot as plt

    import random

    Like

    1. Thank alexis for this. I am sure i will be using this soon…thanks

      Like

  5. […] Simple pyQT and MatplotLib Example with Zoom/Pan […]

    Like

  6. Did anyone get this example with PySide working (alexis’ answer)? – in my case the NavigationToolbar is making troubles:

    Here my ERROR Message:

    QtWidgets.QToolBar.__init__(self, parent)
    TypeError: arguments did not match any overloaded call:
    QToolBar(str, QWidget parent=None): argument 1 has unexpected type ‘Window’
    QToolBar(QWidget parent=None): argument 1 has unexpected type ‘Window’

    Like

  7. Excellent post. This covered all of the salient points in an easy-to-follow manner and showed some pretty non-trivial functionality!

    Thank you so much!

    Like

    1. Thanks for reading and the kind comment.

      Like

  8. Had to make slight changes to get to work under PySide install of PyQt4. See below:

    “””

    From: https://sukhbinder.wordpress.com/2013/12/16/simple-pyqt-and-matplotlib-example-with-zoompan/

    with modifications to make work with pyside from:
    http://matplotlib.org/examples/user_interfaces/toolmanager.html
    Created on Thu Dec 12 08:38:21 2013

    @author: Sukhbinder Singh

    Simple QTpy and MatplotLib example with Zoom/Pan

    Built on the example provided at
    How to embed matplotib in pyqt – for Dummies
    http://stackoverflow.com/questions/12459811/how-to-embed-matplotib-in-pyqt-for-dummies

    “””
    import sys
    if use_pyside:
    from PySide.QtCore import *
    from PySide.QtGui import *
    else:
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    #from PyQt4 import QtGui

    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt4agg import (
    FigureCanvasQTAgg as FigureCanvas,
    NavigationToolbar2QT as NavigationToolbar)
    import matplotlib.pyplot as plt

    import random

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

    self.figure = plt.figure()
    self.canvas = FigureCanvas(self.figure)

    self.toolbar = NavigationToolbar(self.canvas, self)
    self.toolbar.hide()

    # Just some button
    self.button = QtGui.QPushButton(‘Plot’)
    self.button.clicked.connect(self.plot)

    self.button1 = QtGui.QPushButton(‘Zoom’)
    self.button1.clicked.connect(self.zoom)

    self.button2 = QtGui.QPushButton(‘Pan’)
    self.button2.clicked.connect(self.pan)

    self.button3 = QtGui.QPushButton(‘Home’)
    self.button3.clicked.connect(self.home)

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

    def home(self):
    self.toolbar.home()
    def zoom(self):
    self.toolbar.zoom()
    def pan(self):
    self.toolbar.pan()

    def plot(self):
    ”’ plot some random stuff ”’
    data = [random.random() for i in range(25)]
    ax = self.figure.add_subplot(111)
    ax.hold(False)
    ax.plot(data, ‘*-‘)
    self.canvas.draw()

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

    main = Window()
    main.setWindowTitle(‘Simple QTpy and MatplotLib example with Zoom/Pan’)
    main.show()

    sys.exit(app.exec_())

    Like

Leave a comment

Trending