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

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.

Leave a Comment