[matplotlib-devel] Fwd: pylab.close() gtk error. Could be a bug (?)

2006/2/24, David Huard <david.huard@...287... >: What if I

    >>> want it to be non-interactive ? I want to save graphics
    >>> directly to a file from a script, without them being
    >>> displayed. It's weird that in this case close() crashes the
    >>> whole thing.

    > Yes your wright the script have to work in non interactive
    > mode. (I was just trying to give an explanation to Emmanuel
    > problem). The following script should work (but it crashes
    > like Emmanuel reported):

    > #!/usr/bin/python import matplotlib import pylab #Turn off
    > interactive mode matplotlib.interactive(False) #plot figure
    > 1 pylab.figure() pylab.plot([1,2,3],[1,2,3])
    > pylab.savefig("test_pylab1.png") pylab.close() #plot figure
    > 2 pylab.figure() pylab.plot([1,2,3],[1,2,3])
    > pylab.savefig("test_pylab2.png") pylab.close()

This is a bug and I fixed it in CVS (Steve take a look and see if you
agree with the show._mainloop approach) but basically you don't want
to use matplotlib this way.

If you do not want the figure to pop-up, that is you only want to
create a PNG and move on, just use the Agg backend rather than GTK*.


python myscript.py -dAgg

The problem you experienced arose because you closed all the figures
before you called show in non-interactive mode. For a GUI this makes
no sense, because you are telling the GUI to close all the windows
before it shows any of them.

I fixed the GTK backend to not try and quit gtk before starting it :slight_smile:

In summary, I recommend:

  * in non-interactive mode with a GUI backend, always end your script
    with show. This starts the GUI mainloop. Do not close all your
    figures before you call show.

  * if you don't want an image to pop up, use non-interactive mode,
    and a non-GUI backend, and show is not required but it doesn't
    hurt either.