switching output formats within a function

The matplotlib docs say you need to specify the backend

    > before importing matplotlib.matlab. But this seems a bit
    > restrictive: what if I want to display a plot on screen,
    > and then output the same plot to postscript and print it?
    > Normally imports are done only once at the top of a file,
    > but I'd like to be able to switch backends anywhere.

The reason you need to specify the backend first is because everything
from making a figure window to mapping an RGB tuple to a color is
backend dependent. The matlab interface wouldn't know what to do with
the 'figure' command without knowing its backend.

What I think would be useful would be able to instantiate any backend
figure with a figure instance from another backend. Eg a backend
factory which did something like

  figPS = backend_factory(fig, 'PS')
  figGD = backend_factory(fig, 'GD')

Ie, you could initialize a figure in any backend with an instance from
another figure.

This probably will never work perfectly across all backends, primarily
because the GTK and WX backends both have a mainloop that they enter
and it would be difficult to run both at the same time (though perhaps
possible with GUI thread). But in most cases you wouldn't want too.

But there is no reason you shouldn't be able to create a PS figure or
a GD figure to save. I've been meaning to add a 'PS' extension
checker in the savefig command that would enable you to save to PS
from any backend.

Is this primarily what you need to switch backends for?

John Hunter

Yes that's the sweet spot.

Presumably the Save button on the GTK/WX GUI just calls savefig()? In
which case you'd be able to save postscript from there too, which would
be popular too I think.

Another thing that would be handy is a simple cross-platform recipe (or
function call?) for spawning persistent plot windows that don't block
execution of the calling script. It may have been on this list (I should
really search before asking) but it should be in the matplotlib docs I
think.

Oh yeah, and a default keyboard shortcut (or three! Esc, Q, Ctrl-Q) for
quitting the plot window.

And, being able to put the legend outside the plot area (either within a
subplot, or outside all the subplots if the legend is the same for all). A
big legend tends to cover up the data.

Cheers,
Matthew.

···

On Mon, 5 Jan 2004, John Hunter wrote:

    > The matplotlib docs say you need to specify the backend
    > before importing matplotlib.matlab. But this seems a bit
    > restrictive: what if I want to display a plot on screen,
    > and then output the same plot to postscript and print it?
    > Normally imports are done only once at the top of a file,
    > but I'd like to be able to switch backends anywhere.

But there is no reason you shouldn't be able to create a PS figure or
a GD figure to save. I've been meaning to add a 'PS' extension
checker in the savefig command that would enable you to save to PS
from any backend.

Is this primarily what you need to switch backends for?