Create plots in functions, plot later


i try to write a module which evaluates various Data, and occasionally
creates a plot, which is returned to the user. At the discretion of the
user, he should be able to show selected plots. This is an example
module with two approaches for this problem:


import pylab
import matplotlib

def makeplot1():
    fig = pylab.figure()
    spl1 = fig.add_subplot(211)
    spl2 = fig.add_subplot(212)
    return fig

def makeplot2():
    fig = matplotlib.figure.Figure()
    spl1 = fig.add_subplot(211)
    spl2 = fig.add_subplot(212)
    return fig

The user fires up ipython and imports my module:
ipython -pylab

import plotgen

After analysizing various data, he wants to look at a particular plot:

fig = plotgen.makeplot1()

Now can one make pop up this plot? I tried to make this figure current:


but nothing to see. Then:


Only show() works, but this pops up all figures defined, and this can be
a large number of figures if makeplot1() has been called often, or I
have many figures. I only want to see this particular figure.

So next try with the object oriented interface:

fig = plotgen.makeplot2()

AttributeError: Figure instance has no attribute 'number'

Ok, this does not work. In a cookbook i saw that one needs something
like "canvas = FigureCanvasAgg(fig)". But i cannot use it: As writer of
module "plotgen" i dont know what backend (Agg) is used by the user. And
the user simply uses "ipython -pylab" and should not care about the

Whats the correct solution for my problem?

Best Regards,