example use with wxPython

John Wohlbier writes:


I am building a wxPython app, and would like to try
incorporating matplotlib, even though the wx backend
is reportedly in its development state.
Could someone please give a snippet of code using
the wx backend? Also, do I need pygtk even though I
intend on using wxPython?

Hi John,

I'll answer your second question first: wxPython uses
the 'native' widget set of whichever platform you are using,
so if your platform is Windows or Mac you won't need gtk at
all. If your platform is Linux or other Unix/BSD variant,
I believe that wxPython depends on gtk, but not pyGtk
(gtk has been used as the 'native' Unix widget set...)

The answer to your first question is... it depends on
what you are trying to do.

I have modified the 'embedding_in_gtk.py' example to work
under wxPython - it hasn't yet made it into CVS (it will
shortly :slight_smile:

It's about as simple as things get. If you don't want a
navigation toolbar, simply leave out the calls to the
toolbar. I've put the Figure (and toolbar) into a Sizer,
as it's the easiest way to manage things.



P.S. In the example I've commented out the toolbar as I made
a mistake when I checked in __init__.py in the backends
directory, and the toolbar doesn't work right now. It will
be fixed tomorrow, if this matters.



import matplotlib
from matplotlib.backends import Figure
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 = Figure(self, -1, (5,4), 72)
    #self.toolbar = Toolbar(self.fig)
    sizer = wxBoxSizer(wxVERTICAL)
    sizer.Add(self.fig, 0, wxTOP)
    #sizer.Add(self.toolbar, 1, wxGROW)
  def plot_data(self):
    a = Subplot(self.fig, 111)
    t = numpy.arange(0.0,3.0,0.01)
    s = numpy.sin(2*numpy.pi*t)
if __name__ == '__main__':
  app = wxPySimpleApp()
  frame = PlotFigure()

In a larger program, it is probably simplest to create
a class derived from wxPanel to encapsulate the plotting
functionality. The skeleton would be something like:

class PlottingPanel(wxPanel):
  def __init__(self, parent, id=-1)
    wxPanel.__init__(self, parent, id)
    self.fig = Figure(self, figsize=..., dpi=...)
    # Add a toolbar if you want...
    # Put the Figure into a sizer (toolbar as well, if needed)
    sizer = wxBoxSizer(...)
    sizer.Add(self.fig, ...)
    self.n_plot = 1

  def add_subplot(x_list, y_list):
    assert len(x_list) == len(y_list), "Data sets must be same length"
    a = Subplot(self.fig, n*111)
    a.plot(x_list, y_list)

For what it's worth, backend_wx is quite usable for basic plotting. There are a few
oddities in the operation of the toolbar, which I haven't quite sorted out, and there
is not yet the ability to save a plot as a graphic. I develop on wxPython for Win32
for a data analysis project which is the reason I started on backend_wx, but regression
test everything on a Debian box (hence gtk) at home. You will need to use either a
working snapshot (I believe John posted one a few days back), or keep a careful eye on
the CVS. John is making some very worthwhile improvements in basic functionality, but
I track these a day or so later in backend_wx (this makes my job much simpler as I
basically just copy what John has done and 'translate' it to wx!)

As of writing, the example above works with CVS. In a day or two it will appear as part
of the matplotlib examples (probably with more comments!). For now it should get you


John Wohlbier writes:
I ended up downloading the latest CVS using the command
cvs -z3 -d:pserver:anonymous@...55...:/cvsroot/matplotlib co

and doing python setup.py install

Now when I run my application (which worked with version 0.32) I get
this error on startup


ImportError: cannot import name FigureManagerBase

You're not doing anything wrong - there have been some recent changes to
the API which have moved FigureManagerBase. The example I posted just
now does things a different way (and does work with latest CVS, honest!)

By the way, when I run the python setup.py install command will
the script properly overwrite the existing version of the matplotlib
library, or should I go in and delete the old library first?

It updates things very nicely - no need to delete anything.



Thanks for the help. Indeed I would like the toolbar eventually.
Please let me know when it is fixed.



P.S. In the example I've commented out the toolbar as I made
a mistake when I checked in __init__.py in the backends
directory, and the toolbar doesn't work right now. It will
be fixed tomorrow, if this matters.