Simple pyQT and MatplotLib Example with Zoom/Pan

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.

Advertisements

11 thoughts on “Simple pyQT and MatplotLib Example with Zoom/Pan

  1. Pingback: Matplotlib with wxPython example with Pan/Zoom functionality | SukhbinderSingh.com

  2. Pingback: Matplotlib Embedded with Tkinter. | SukhbinderSingh.com

  3. 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

  4. Pingback: A year of posts | SukhbinderSingh.com

  5. 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

  6. 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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s