Remote display in python? [Ask Astrobetter]

by Jane on December 31, 2012

The question:  “How can I remotely display python graphics windows?”

The backstory:  I’m gotten used to the nifty interactive graphics of matplotlib in ipython.  But here it is New Year’s Eve and I’m working from home.  I need to ssh in to my desktop, run python, and remotely display the python graphics windows to my laptop.  This is easy to do in, say, gnuplot (ssh in, and “set terminal x11″), but an hour into reading horrible python docs and stackoverflow, I can’t figure out how to remotely display a python window.

A promising avenue, matplotlib.use(‘GTK’) failed because I can’t get pygtk to install.

{ 17 comments… read them below or add one }

1 John December 31, 2012 at 12:51 pm

Without knowing a little more detail about your setup, it’s hard to be completely concrete. But: basically any matplotlib interactive backend with the exception of “macosx” should work easily over an X11 connection. In other words, if:

1. You have a X server running on your laptop (which you do, if you can run gnuplot);
2. You have X11 forwarding enabled in SSH (as above);
3. When sitting at your desktop, which isn’t a Mac, you can use matplotlib interactively,

then everything should “just work”.

If your desktop is a Mac, you will probably need to switch the backend to something non-default. It’s a fair bet that the TkAgg backend is installed, and will work just as well as GTK. Try matplotlib.use(“TkAgg”), or, equivalently, invoking:

$ ipython –pylab=tk

If that doesn’t help, maybe you could provide some more details about the platform your running on and what, if any, errors are reported.

Reply

2 Meg Schwamb December 31, 2012 at 1:25 pm

Hi
I was using python remotely from my work desktop on my mac last night and it was displaying images the graphics over the connection. I didn’t do anything different than what I normally do to log in and forward the display. It sounds more like an X11 forwarding issue possibly. Does showing doing something like xlogo & in the remote terminal pop up a little white window? If not then you’re not forwarding the X11 along for all programs not just python.
Happy New Year,
~Meg

Reply

3 Yannick December 31, 2012 at 1:28 pm

Have you tried to use your local ipython but connecting it to the remote ipython kernel via ssh as explained there? → http://wiki.ipython.org/Cookbook/Connecting_to_a_remote_kernel_via_ssh

Reply

4 Adam Ginsburg December 31, 2012 at 5:43 pm

I think this only works for graphical ipython frontends, i.e. qt console or notebook. @Jane, ipython notebooks are one really nice way to interact with a remote machine, but I don’t think this method will work if you want the terminal version of ipython (or if you have ipython <=0.11)

5 Jim December 31, 2012 at 1:32 pm

I got weary of solving this over and over again from operating system to operating system. So I switched to a remote desktop option. It is not the most efficient if your home internet gets dropouts. But it works for a quick look.

Typically, I prefer to just mirror the data from home and work, and run the applications on the home system.

Reply

6 Jane December 31, 2012 at 1:32 pm

Desktop and laptop are both macs. X11 and forwarding are working fine (“xterm&” makes a new window on the laptop.)

$ ipython –pylab=tk gives me the entire ipython help menu, and then “WARNING: Error in Arguments: “Argument ‘-pylab=tk’ occurs multiple times”.

While matplotlib.use(“TkAgg”) produces this amusing error within ipython:
In [12]: pylab.plot(x.y)
Out[12]: [matplotlib.lines.Line2D object at 0x102e3ef50>]

Reply

7 John December 31, 2012 at 1:49 pm

First off: note that you need two hyphens in front of the “pylab” argument to IPython. It looks like the two dashes in my example above have been munged into an en-dash, which won’t work if you copy and paste. That said, it’s possible your version of IPython is too old for this syntax anyway.

Anyway, the output (not an error!) you quote looks fine. Following the commands above, try simply entering “pylab.show()” — does that do anything?

8 Jane December 31, 2012 at 1:59 pm

“Does that do anything?” Yup, it brings up a plot on the desktop. (I checked by VNC…)

Reply

9 John December 31, 2012 at 2:51 pm

Weird — no easy answer to that. Are you really running IPython/pylab with exactly the same environment (if possible, in the same terminal, and without running screen or similar) as you can successfully invoke an xterm and have it display on your laptop?

Try putting the following in a python script and running that in the same terminal:

import os
print “Display is: %s” % (os.environ['DISPLAY'],)
import matplotlib
print “Default backend is: %s” % (matplotlib.get_backend(),)
matplotlib.use(‘TkAgg’)
print “Backend is now: %s” % (matplotlib.get_backend(),)
from matplotlib import pyplot
pyplot.ioff()
pyplot.plot([1,2,3])
pyplot.show()

(save as eg test.py and invoke as “python test.py”). What’s the output?

10 Adam Ginsburg December 31, 2012 at 5:40 pm

@Jane, @John – I believe the X-forwarding issue has to do with how Tk was installed. You can compile Tk such that it uses Mac-native display windows. @Jane, what icon shows up when you run python? I think if it shows up as anything but part of X11, you can’t get it to forward, but I don’t know.

I always use VNC for python display, but I tend to run my interactive python terminal from a gnu-screen session, and ssh into my remote machine. So I enter my commands into my local terminal (which is faster and renders more nicely), but I see the graphics windows popping up on the remote machine.

Reply

11 John December 31, 2012 at 9:11 pm

Adam writes:

> You can compile Tk such that it uses Mac-native display windows

And I’m sure he’s correctly identified the problem. So, Jane, that means your options are:

1. Recompile Tk so that it uses X11 rather than Quartz;
2. Use a different backend which uses X11 (GTK, WX, Qt or FLTK, I guess);
3. Forget X11, and just use VNC or similar.

Assuming you didn’t build Tk yourself to begin with but rather are relying on a package provided by somebody else, option 1 is likely tiresome (although see the note on Macports, below).

You’ve already tried option 2, and run into problems with getting GTK going. You could investigate the other toolkits, but I can’t promise which will be easier.

Given that you’ve already seen the plot appearing on your work desktop by VNC, I’m inclined to think that option 3 is the easiest in the short term, at least.

By the way, if you happened to use Macports (see Astrobetter posts passim) to configure your system, switching Tk to use X11 is a snap. All you need is:

$ sudo port upgrade –enforce-variants tk -quartz +x11

12 Adam Ginsburg January 1, 2013 at 11:41 am

BTW, I checked, and non-X11 screen forwarding seems impossible: http://apple.stackexchange.com/questions/76422/display-forwarding-for-non-x-programs

13 TMB January 3, 2013 at 1:48 am

Related question… anyone have a suggestion how to get pyraf to use an X11-forwardable backend when run on a mac?

14 Carlos January 1, 2013 at 4:21 pm

Hi Jane,

Did you try the WX device (backend) already. It seems to work just fine for me. “import matplotlib; matplotlib.use(‘wx’); import matplotlib.pyplot as plt; plt.plot(x,y); plt.show()”. Also, please, try to connect using the ‘-Y’ option for your SSH connection in place of the usual ‘-X’.
Hope it helps.

Happy new year from Brazil ;)
[]

Reply

15 Carlos January 1, 2013 at 4:23 pm

* First frase == question (?)
Sorry.

16 Colette May 3, 2013 at 9:08 pm

Did anyone ever figure this out? I’m now trying the same and appear to be having the exact same issues as Jane. I’m running a MacPorts installation that uses native Mac displays which cannot be forwarded. So I’m unable to display matplotlib graphics via ssh. Tried several of the suggestions above but also no luck. I’m still resisting the VNC option and don’t quite know enough to do something more clever than installing a pre-packaged python like the MacPorts version.

Reply

17 Jim Doyle November 17, 2013 at 2:06 am

Yes, it works for me.

I’m using tightvncserver on a Raspberry Pi. I used this as a test – works fine – saw the plot on my Windows Vista based vnc client… the matplotlib.use(‘GTK’) might be key. I started ipython like this:
the –pylab option is key!

root@Raspyfi:/usr/bin/X11# ipython –pylab

then:
In [1]: import numpy as np
In [2]: import matplotlib
In [3]: matplotlib.use(‘GTK’)
In [4]: x=np.linspace(0,3*np.pi, 500)
In [5]: import matplotlib.pyplot as plt
In [6]: plt.plot(x, np.sin(x**2))

Reply

Leave a Comment

Previous post:

Next post: