How to place a canvas in a scrolledwindow and change its size?

Dear matplotlib users,
I am a new enthusiastic member of the matplotlib community.

I'll start up my frequentation in the ml with two questions for which I
couldn't find an answer; the first one is relatively tricky while the second
one should be quite straigthforward. Both are, I believe, of general
interest.

1) I use matplotlib and the wxmpl library to wrap the graphs in a notebook
page of a wxpython application. My canvas is a wxmpl.PlotPanel which is a
derivation of FigureCanvasWxAgg.Everything works just great, I love it.
Thing is that while most graphs have contained size, certain graphs can be
actually composed of several subplots and therefore I would like them to be
plotted on bigger canvas.

I can create a big canvas in a scrolledwindow with big virualsize and this
works just fine.
What I cannot do is to dinamically resize the canvas after it has been
create, ie. Create a smaller canvas and then increase its size only if
needed.

One more thing is that if I put the canvas in a sizer and then set the
scrolledwindow to fit the sizer than the canvas changes its dimension
accortding to the dimension of the frame (meaning if I maximize the whole
frame I seem to get a bigger canvas.
Here some graphic examples:

This is what I have now

Class MyNoteBookpage(wx.panel):
  def _init_ ...
    #In the notebook page self, create scrolledwindow
    self.virtualw = wx.ScrolledWindow(self)
    self.virtualw.SetVirtualSize((1000,1000))
    self.virtualw.SetScrollRate(20,20)
    
    #Create Canvas, child of the scrolledwindow
    self.canvas1 = MyCanvas(self.virtualw)

    #now arrange the sizer
    self.cs = wx.BoxSizer()
    self.cs.Add (self.canvas1, 1, wx.GROW|wx.ALL, 1)
    self.virtualw.SetSizer(self.cs)

This code will result in this: http://zipp.it/u/Q687P
Note that in the panel I also have a grid and a textbox and some buttons,
all arranged within a sizer.

If I resize the figure using

  fig.set_size_inches((10,10)) I get this: http://zipp.it/u/Z466Y

2) Is it possible to include in my subplot a custom drawing?
I would like to have a half filled rectangle below the x axis, like the one
you see on panel b of the figure: http://zipp.it/u/L963V

Thanks a lot,
Giorgio

···

--
giorgio@...1462...
http://www.cafelamarck.it

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()