Matplotlib with wxPython example with Pan/Zoom functionality

wxPython Zoom As this post said, I don’t enjoy coding with pyQt, but somehow wxPython escapes that curfew.

What is wxPython?
Like pyQt, wxPython is a GUI toolkit for the Python programming language. It allows Python programmers to create programs with a robust, highly functional graphical user interface, simply and easily. It is implemented as a Python extension module (native code) that wraps the popular wxWidgets cross platform GUI library, which is written in C++.

So given a choice in getting a quick GUI, wxPython is my first choice over pyQt.

After doing the zoom/ pan functionality with matplotlib embedded in qt, I wanted to do the same with wxPython. So here’s the code for embedding matplotlib with wxPython with zoom/ pan functionality.

Created on Thu Dec 12 11:20:52 2013

@author: Sukhbinder Singh

Matplotlib and WxPython with Pan/Zoom functionality


import wx
import random

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar
import matplotlib.pyplot as plt

class p1(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self, parent)
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self,-1, self.figure)
        self.toolbar = NavigationToolbar(self.canvas)
    def plot(self):
        ''' plot some random stuff '''
        data = [random.random() for i in range(25)]
        ax = self.figure.add_subplot(111)
        ax.plot(data, '*-')

class TestFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,title=title,size=(650,600), style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|
        self.sp = wx.SplitterWindow(self)
        self.p1 = p1(self.sp)
        self.p2 = wx.Panel(self.sp,style=wx.SUNKEN_BORDER)

        self.statusbar = self.CreateStatusBar()
        self.plotbut = wx.Button(self.p2,-1,"plot", size=(40,20),pos=(160,10))
        self.sibut = wx.Button(self.p2,-1,"Zoom", size=(40,20),pos=(60,10))
        self.hmbut = wx.Button(self.p2,-1,"Home", size=(40,20),pos=(110,10))
        self.hibut = wx.Button(self.p2,-1,"Pan", size=(40,20),pos=(10,10))
    def zoom(self,event):

    def home(self,event):
    def pan(self,event):

    def plot(self,event):

app = wx.App(redirect=False)
frame = TestFrame(None,"Matplotlib and WxPython with Pan/Zoom functionality")

Hope this helps someone.

7 thoughts on “Matplotlib with wxPython example with Pan/Zoom functionality

  1. Pingback: Lights, Cameras, Action : A Simple Example to Embed VTK with wxPython |

  2. Pingback: SLT Viewer – a VTK and wxpython powered App |

  3. Pingback: Simple and Minimal File Selection GUI with Standard Python |

  4. Pingback: Matplotlib Embedded with Tkinter. |

  5. wow !! it’s just what I need,

    Is it possible to add functionality to record an image of the graph?
    Thank you!!! it helps me a lot.

  6. Pingback: ex01-wxPython+matplotlib – OpenCAE 열린공학

Leave a Reply

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

You are commenting using your 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