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)
        self.toolbar.Hide()
    
    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()    
      

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|
                  wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
        self.sp = wx.SplitterWindow(self)
        self.p1 = p1(self.sp)
        self.p2 = wx.Panel(self.sp,style=wx.SUNKEN_BORDER)
        
        self.sp.SplitHorizontally(self.p1,self.p2,470)

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

    def home(self,event):
        self.statusbar.SetStatusText("Home")
        self.p1.toolbar.home()
        
    def pan(self,event):
        self.statusbar.SetStatusText("Pan")
        self.p1.toolbar.pan()

    def plot(self,event):
        self.p1.plot()       

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




Hope this helps someone.

Advertisements

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

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

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

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

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

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

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