Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a figure remotely with pylab? [duplicate]

I'm trying to generate a figure at a remote computer with the command pylab.savefig. But I got such error:

Unable to access the X Display, is $DISPLAY set properly? 

How can I save the figure properly?

like image 694
gerry Avatar asked Jan 16 '11 16:01

gerry


People also ask

How do I save a specific figure in Matplotlib?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

How do I save a figure without showing in Matplotlib?

We can simply save plots generated from Matplotlib using savefig() and imsave() methods. If we are in interactive mode, the plot might get displayed. To avoid the display of plot we use close() and ioff() methods.

Is PyLab the same as Matplotlib?

PyLab is a procedural interface to the Matplotlib object-oriented plotting library. Matplotlib is the whole package; matplotlib. pyplot is a module in Matplotlib; and PyLab is a module that gets installed alongside Matplotlib. PyLab is a convenience module that bulk imports matplotlib.


1 Answers

By default, matplotlib will use something like the TkAgg backend. This requires an X-server to be running.

While you can just use X-forwarding, there will be a noticeable lag as matplotlib tries to connect with the remote X-server. If you don't need to interact with the plot, it's often nicer to speed things up by avoiding an X-connection entirely.

If you want to make a plot without needing an X-server at all, use the Agg backend instead.

E.g. do something like this:

import matplotlib matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab! import matplotlib.pyplot as plt  fig = plt.figure() plt.plot(range(10)) fig.savefig('temp.png') 

If you want this to be the default behavior, you can modify your matplotlibrc file to use the Agg backend by default.

See this article for more information.

like image 61
Joe Kington Avatar answered Oct 11 '22 22:10

Joe Kington