Plotting in batch with no display

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Fri Jun 4 18:27:33 EDT 2010


On 08:01 pm, h.schaathun at surrey.ac.uk wrote:
>Admittedly not the strongest reason, but yet an important one,
>for switching from Matlab to python/numpy/scipy/matplotlib,
>is that Matlab is very cumbersome to run in batch.
>
>Now I discover that some of the matplotlib.pyplot functions
>(incl. plot and contour) insist on opening an X11 window
>(just like Matlab does).  I would have preferred just to create
>the plot straight on file.  The extra window is a nuisance on my
>laptop, it is deep-felt pain if I try to run it remotely.  It fails
>completely if I run it without any display at all.
>
>Oddly, the bar() function does not open a window by default.
>I was very happy with that.  It works exactly the way I want.
>(Why isn't pyplot more consistent?)
>
>Is there something I have missed?  Is it possible to create
>standard 2D plots and contour plots without a display, writing
>the graphics straight into a PDF file?

It's possible to plot with matplotlib without a display.  I'm not 
surprised you didn't figure out how, though, it's not all that obvious.

Check out the matplotlib.use function.  For example:

    import matplotlib
    matplotlib.use('agg')
    import pylab
    pylab.plot([1, 3, 5])
    fig = file('foo.png', 'wb')
    pylab.savefig(fig, format='png')
    fig.close()

This should work fine without a display.

Jean-Paul



More information about the Python-list mailing list