Changing Backend Frustration

Not everyone uses matplotlib inside of scripts! Judging

    > from the manual, this is the only approved way to use the
    > library.

Hmm. Not quite true. I hear there is a fellow in Boulder who
sometimes uses matplotlib interactively <wink>. If the manual gives a
different impression, that is unintentional, and may result from
selective reading. Did you read section 1.5 with subheading
"Interactive"? Eg,

   The recommended way to use matplotlib interactively from a shell is
   with ipython, which has an pylab mode that detects your matplotlib
   .matplotlibrc file and makes the right settings to run matplotlib
   with your GUI of choice in interactive mode using threading. gtk
   users will need to make sure that they have compiled gtk with
   threading for this to work. Using ipython in pylab mode is
   basically a nobrainer because it knows enough about matplotlib
   internals to make all the right settings for you internally.

Fernando and I have spent a lot of time and effort to make matplotlib
work seamlessly in interactive use. The issues are non-trivial
because you have to handle threading to keep the GUI from stealing the
show. Because threading is always involved when using most GUIs from
a python shell, we also provide an example in examples/interactive.py
which three mpl developers collaborated on to show people the
beginnings of how to roll their own custom interactive GTK shell.

I've also added a lot of short aliases for long keywords so you can
do, for example

>>> plot([1,2,3], ls='--', c='red')

instead of

>>> plot([1,2,3], linestyle='--', color='red')

Basically, the claim that we only support a scripting interface is not
true. If you have specific things to suggest to further improve
interactive use, or documentation suggestions, fire away.

    > There seems to be no way to change the backend in the middle
    > of an interactive session. Looking through the manual, I
    > find that I can specify it on the command line, in
    > .matplotlibrc, or via matplotlib.use('..'), but only "before
    > you import matplotlib.pylab"

matplotlib development usually happens when a developer needs a
feature or one or more users make a case that a feature is important
-- you are officially the second person to request the ability to
switch backends interactively, if memory serves.

The first person was Fernando Perez, who has driven many of the
improvements in interactive use because he makes lots of suggestions
and contributes code. Recently on matplotlib-devel, we discussed the
importance of being able to switch backends interactively
http://sourceforge.net/mailarchive/message.php?msg_id=10724264 and I
provided a pylab_interface function switch_backend which does just
that. As you'll see in that thread, Fernando is looking into ways to
include the backend switching functionality into ipython, eg so you
can run a script with

  >>> run -backend PS somefile.py

But even w/o this ease of use, in the current matplotlib release the
pylab switch_backend function exists. It's not documented because we
are still exploring and testing it and it is experimental. But I
added a docstring this morning which I'll paste in here

def switch_backend(newbackend):
    """
    Swtich the default backend to newbackend. This feature is
    EXPERIMENTAL, and is only expected to work switching to an image
    backend. Eg, if you have a bunch of PS scripts that you want to
    run from an interactive ipython session, yuo may want to switch to
    the PS backend before running them to avoid having a bunch of GUI
    windows popup. If you try to interactively switch from one GUI
    backend to another, you will explode.

    Calling this command will close all open windows.
    """

    > I would be extremely happy if there were a way to change
    > backends midstream from an interactive shell (I use ipython,
    > for example)

Good. You should be extremely happy then. As I suggested above, this
feature is lightly tested, so let us know what you find.

JDH