I asked some help last week.
I post here the solution to my problem since I believe it's something of
general interest.
A figure is generated and the user can change the size of the canvas (in
inches) interactively, as if we are zooming in and out the canvas.
This is particularly useful when you are drawing figures that can have few
or many subplots.

I couldn't manage to get this working with a resize so the trick is that
every time we destroy and create the canvas.

Cheers.

-------------------------------------------------------------
import wx, wxmpl
from numpy import *


class PanelWScrolledWindow(wx.ScrolledWindow):
        def __init__(self,parent):
                wx.ScrolledWindow.__init__(self, parent)

                self.mainsizer = wx.BoxSizer(wx.VERTICAL)
                self.BTNsizer = wx.BoxSizer(wx.HORIZONTAL)
                self.width, self.heigth = 3, 6

                BTNsmall = wx.Button(self, -1, "Smaller FIG")
                BTNbig = wx.Button(self, -1, "Bigger FIG")

                self.Bind(wx.EVT_BUTTON, self.onBTNbig, BTNbig)
                self.Bind(wx.EVT_BUTTON, self.onBTNsmall, BTNsmall)
                self.BTNsizer.Add (BTNsmall, 0, wx.GROW|wx.ALL, 1)
                self.BTNsizer.Add (BTNbig, 0, wx.GROW|wx.ALL, 1)
                #Note that in a real world you want the button to be outside
the scrolledwindow

                self.mainsizer.Add (self.BTNsizer, 0, wx.GROW|wx.ALL, 1)

                self.CreateCanvas()


        def CreateCanvas(self, size=(2,4)):
                try:
                        self.canvas.Destroy()
                except:
                        pass
                
                self.canvas = wxmpl.PlotPanel(self, -1, size)
                self.SetScrollbars(20, 20, size[0]/20, size[1]/20)
                self.mainsizer.Add (self.canvas, 1, wx.GROW|wx.ALL, 1)
                self.SetSizer(self.mainsizer)
                self.FitInside()
                
        def onBTNbig(self,event):
                self.heigth += 1
                self.width +=1
                self.CreateCanvas(size=(self.width,self.heigth))
                plot_simple(self.canvas.get_figure())

        def onBTNsmall(self,event):
                self.heigth -= 1
                self.width -=1
                self.CreateCanvas(size=(self.width,self.heigth))
                plot_simple(self.canvas.get_figure())


class MyFrame(wx.Frame):
        def __init__(self, parent, title):
                wx.Frame.__init__(self, parent)

                self.nb = wx.Notebook(self)

                self.PageOne = PanelWScrolledWindow(self.nb)
                self.PageTwo = wx.Panel(self.nb)

                self.nb.AddPage(self.PageOne, "PageOne")
                self.nb.AddPage(self.PageTwo, "PageTwo")

def plot_simple(fig):

        t = arange(0.0, 2.0, 0.01)
        s = sin(2*pi*t)
        c = cos(2*pi*t)
        
        axes = fig.gca()
        axes.plot(t, s, linewidth=1.0)
        axes.plot(t, c, linewidth=1.0)
        
        axes.set_xlabel('time (s)')
        axes.set_ylabel('voltage (mV)')
        axes.set_title('About as simple as it gets, folks')
        axes.grid(True)

#here we start
app = wx.PySimpleApp()
frm = MyFrame(None, "Test")
frm.SetSize((800,600))
frm.Show()
app.SetTopWindow(frm)
app.MainLoop()


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to