78

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?

gerry
  • 1,539
  • 1
  • 12
  • 22
  • 1
    Related: http://stackoverflow.com/questions/2801882/generating-a-png-with-matplotlib-when-display-is-undefined?lq=1 – 0 _ Feb 28 '14 at 05:47

2 Answers2

153

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.

Wael
  • 1,640
  • 1
  • 9
  • 20
Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • 2
    I had the same problem with gerry. For me, Joe's trick alone is not enough, I also needed to do ssh -X nos@server.com instead of just ssh – nos Mar 12 '12 at 22:04
  • matplotlib.use('Agg') did not work for me but changing the backend default to Agg in the matplotlibrc file (located at /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc ) worked for me – Orlando Nov 09 '16 at 10:01
  • 1
    still I have the runtime error :( File "/home/user/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp raise RuntimeError('Invalid DISPLAY variable') RuntimeError: Invalid DISPLAY variable – Hana90 Apr 01 '17 at 21:09
  • 1
    @Hana90: `matplotlib.use` only works before importing matplotlib.pyplot in any way. Is it possible that you've already imported pyplot? That includes things like `pylab` or other libraries that import pyplot. It also includes an active ipython session, or if you have any auto imports set up. Try a minimal example run with bare `python` from a command line. – Joe Kington Apr 01 '17 at 21:46
  • Thanks I have to connect ssh -X server, Thank you – Hana90 Apr 02 '17 at 17:54
  • Briefly 1) look where is you matplotlibrc ``python`` ``>>>import matplotlib`` ``>>>matplotlib.matplotlib_fname()`` '/path/to/matplotlib/matplotlibrc' 2) go and edit your matplotlibrc file find -> ``backend : Tkagg`` replace by -> ``backend : Agg`` – Wael Feb 07 '23 at 09:08
1

Try setting the DISPLAY variable to the appropriate value.

Graphics over the network using X11 work by the client (remote) computer having a DISPLAY environment variable that says where to draw the graphics. Typically it would be something like mydesktop.example.com:0.0 - then when an X11 program tries to draw something, it gets whizzed over the network to mydesktop.example.com, which is the machine you are sitting in front of (the X server) and up it pops.

Now, if the machine in front of you is Windows, then you'll need to get an X server from somewhere - cygwin/X11 or commercial eXceed will do nicely.

You also need to make sure security is handled - you cant just have anyone writing to your screen over the network.

How are you connecting to the remote machine? Because if you are going from a Linux box to another Linux box with ssh then the simple solution is probably 'Use ssh -X foo.example.com' to connect - this pipes the X11 connection over a local socket.

So, if ssh -X isnt the answer, can we have some more info on the operating systems involved please?

swimfar
  • 147
  • 6
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 1
    Thanks for your explanation. I'm ssh a linux machine under windows. Is that possible to make the figure without X11? – gerry Jan 16 '11 at 16:31
  • Not so that it appears on-screen - you'd have to get pylab to write it to a file and then copy that file to your Windows box. Maybe give http://winswitch.org/ a go - that's an easy interface to fancy cross-platform graphics fun. – Spacedman Jan 16 '11 at 16:34
  • I did "ssh -X username@servername" from my linux box to another linux box and it worked – Mahshid Zeinaly Sep 02 '14 at 20:57
  • Hello Spacedman, I got a kind of questions i plot. I need expert advice like you, please have a look http://stackoverflow.com/questions/36248016/how-to-get-more-information-in-2nd-plot-by-clicking-1st-plot-using-python-clicki –  Apr 01 '16 at 07:50