From: Jeremy O'D. <je...@o-...> - 2003-12-02 17:14:17
|
Hi Flavio, I expect that you've found the solution to your problem now, but just in case, I have now published significantly improved version of the wx embedding example in CVS. Since Sourceforge can take some time to sort these things out, I have included the code here. I should also note that there has been quite a significant enhancement to the CVS version of backend_wx committed to CVS. You may wish to use this (although I don't think that the interface has changed anywhere. Details of the changes have been posted to the matplotlib-devel list, or you can see details at the top of the source file (matplotlib-devel archiving appears to be very slow). Regards Jeremy =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D= 3D=3D3D=3D3D=3D3D=3D3D """ Copyright (C) Jeremy O'Donoghue, 2003 License: This work is licensed under the PSF. A copy should be included with this source code, and is also available at http://www.python.org/psf/license.html This is a sample showing how to embed a matplotlib figure in a wxPanel. The example implements the full navigation toolbar, so you can automatica= =3D lly inherit standard matplotlib features such as the ability to zoom, pan and save figures in the supported formats. There are a few small complexities worth noting in the example: 1) By default, a wxFrame can contain a toolbar (added with SetToolBar()) but this is at the top of the frame. Matplotlib default is to put the controls at the bottom of the frame, so you have to manage the toolbar yourself. I have done this by putting the figure and toolbar into a sizer, but this means that you need to override GetToolBar for your wxFrame so that the figure manager can find the toolbar. 2) I have implemented a figure manager to look after the plots and axes. If you don't want a toolbar, it is simpler to add the figure directly and not worry. However, the figure manager looks after clipping of the figure contents, so you will need it if you want to navigate 3) There is a bug in the way in which my copy of wxPython calculates toolbar width on Win32, so there is a tricky line to ensure that the width of the toolbat is the same as the width of the figure. 4) Depending on the parameters you pass to the sizer, you can make the figure resizable or not. """ import matplotlib matplotlib.use('WX') from matplotlib.backends import Figure, Toolbar, FigureManager from matplotlib.axes import Subplot import Numeric as numpy from wxPython.wx import * class PlotFigure(wxFrame): def __init__(self): wxFrame.__init__(self, None, -1, "Test embedded wxFigure") self.fig =3D3D Figure(self, -1, (5,4), 75) self.toolbar =3D3D Toolbar(self.fig) self.toolbar.Realize() # On Windows, default frame size behaviour is incorrect # you don't need this under Linux tw, th =3D3D self.toolbar.GetSizeTuple() fw, fh =3D3D self.fig.GetSizeTuple() self.toolbar.SetSize(wxSize(fw, th)) # Create a figure manager to manage things self.figmgr =3D3D FigureManager(self.fig, 1, self) # Now put all into a sizer sizer =3D3D wxBoxSizer(wxVERTICAL) # This way of adding to sizer prevents resizing #sizer.Add(self.fig, 0, wxLEFT|wxTOP) # This way of adding to sizer allows resizing sizer.Add(self.fig, 1, wxLEFT|wxTOP|wxGROW) # Best to allow the toolbar to resize! sizer.Add(self.toolbar, 0, wxGROW) self.SetSizer(sizer) self.Fit() def plot_data(self): # Use ths line if using a toolbar a =3D3D self.figmgr.add_subplot(111) # Or this one if there is no toolbar #a =3D3D Subplot(self.fig, 111) t =3D3D numpy.arange(0.0,3.0,0.01) s =3D3D numpy.sin(2*numpy.pi*t) c =3D3D numpy.cos(2*numpy.pi*t) a.plot(t,s) a.plot(t,c) self.toolbar.update() def GetToolBar(self): # You will need to override GetToolBar if you are using an # unmanaged toolbar in your frame return self.toolbar if __name__ =3D3D=3D3D '__main__': app =3D3D wxPySimpleApp() frame =3D3D PlotFigure() frame.plot_data() frame.Show() app.MainLoop() |