[SciPy-user] gplt and xplt

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Aug 12 16:36:20 CDT 2004


>>>>> "David" == David M Cooke <cookedm at physics.mcmaster.ca> writes:

    David> Hidden variable; the '1' is a global identifier for the
    David> plot.

OK, good eye.  I was playing a little fast and loose.  However, the
fact remains that you do not need the matlab interface at all.  I was
using it minimally in my example above.

    >> show()

    David> Again, no hidden globals? Then what does show() do? What
    David> does it show?  

It provides a consistent interface so that the same script can
realize figure windows across the 4 GUI backends: GTK, WX, Tkinter and
FLTK.  But you're right again - some pixie dust under the hood.


    David> savefig() has the same problem. They both have the concept
    David> of "current figure" -- which is a global I don't reference
    David> above.  

True, but I didn't call savefig in my example.  See below, in any
case.

    David> In essence, can I build up two plots independently
    David> at the same time? Then save them?

Not a problem - here the matlab interface is not imported or used.
The use of a separate Figure and FigureCanvas below results from
trying to keep a clean separation from the frontend (Figure) and
backend (Canvas)

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.numerix import rand

    fig1 = Figure()
    fig2 = Figure()

    canvas1 = FigureCanvas(fig1)
    canvas2 = FigureCanvas(fig2)

    ax1 = fig1.add_subplot(111)
    ax2 = fig2.add_axes([0.2, 0.2, 0.5, 0.5])  # a custom axes in fig2

    ax1.plot([1,2,3])

    ax2.scatter(rand(12), rand(12))

    canvas1.print_figure('fig1.svg')  # switches to the svg backend
    canvas1.print_figure('fig1.png')  # native agg saves png
    canvas2.print_figure('fig2.eps')  # switches to the PS backend

No call to show, etc....  The hardcopy is created upon executing this
script.  Here I take advantage of the fact that most backends (agg in
this case) can switch backends to render to PS/EPS/SVG/PNG depending
on the extension.  You don't need to use the switching feature - you
could have directly imported the SVG or PS backend canvas, but I
included it for illustration.

You use the same approach to create GUI canvases for embedding in an
application, but the constructor args for the FigureCanvas vary
somewhat from backend to backend.  And you would have to fire up your
GUI mainloop and realize your windows, etc..

Thanks for keeping me honest!  I'm working on a users guide and will
include a section along these lines.

JDH



More information about the SciPy-user mailing list