From: Ryan K. <rya...@gm...> - 2007-03-16 02:21:47
|
Thanks John. I know I have some clean up to do, I just want to do it right so it isn't an annual (or more often) thing.... On 3/15/07, John Hunter <jd...@gm...> wrote: > On 3/15/07, Ryan Krauss <rya...@gm...> wrote: > > > How should I be using matplotlib/pylab in my utility scripts so that > > they are compatible with embedding in wx? > > A good rule of thumb is to never import pylab at the top level for > modules that need to be imported. In my own code, I often do > something like > > def somefunc(figfunc): > fig = figfunc() > ax = fig.add_subplot(111) > ax.plot([1,2,3]) > > and then I can call it with > > somefunc(pylab.figure) > > or a custom func that generates a GUI embedded figure instance. Eg, > in my GTKApps, I have a functor like gtk_figure that returns a > function that creates a figure embedded in a GTK window. > > In basemap, Jeffrey Whitaker does something like the following > > def somefunc(ax=None): > if ax is None: > import pylab > ax = pylab.gca() > > Here the pylab import is triggered only when the function is called > with default arguments. That way you can use it from GUI code without > triggering a pylab import like > > somefunc(ax) > > and from other code where you want pylab do do everything with > > somefunc() > > I'm afraid you have some cleanup to do..... Mixing pylab with > embedded GUI code is a recipe for pain and misery. > > JDH > |