6

I have a headless Ubuntu 14.04 Server that I connect to remotely using SSH. I want to use matplotlib and have plots appear at the ssh client. For example, I would connect using:

ssh -X name@server.com

And then from a Python console, I want this to produce a plot in a window:

import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()

I have installed matplotlib in my virtualenv, and I ran sudo apt-get install python-gtk2, but the plot still doesn't appear. I assume I'm missing lots of packages. What is a fairly minimal set of X-related packages I could install to make this work? I do NOT want to install ubuntu-desktop.

Scott
  • 2,764
  • 1
  • 16
  • 16
  • also https://stackoverflow.com/questions/2801882/generating-a-png-with-matplotlib-when-display-is-undefined – tacaswell Nov 07 '14 at 22:08
  • That is not the same question. I explicitly WANT to use an X backend, while the accepted answer on the other question works around that requirement. – Scott Nov 07 '14 at 22:10
  • 1
    Sorry about that, the other question comes up so often I pattern-matched too aggressively. Can you get other gui programs to launch and do you have an xserver running on the remote machine? – tacaswell Nov 07 '14 at 22:14
  • Thanks for untagging! After some trial and error with x-related packages, I can get xeyes to run over ssh. The problem seems to be that when I `pip install matplotlib`, it doesn't recognize that pygtk is installed. Therefore, the only backend that gets installed is agg. The problem might be that pygtk is installed site-wide rather than in my venv, but I have site packages enabled for this venv. Thus, `import pygtk` works fine. But the pip installer doesn't seem to realize I have it installed. – Scott Nov 07 '14 at 22:18
  • 1
    @10flow - It's possible the auto-detection of which backends to build is being rather quirky in your specific case. A workaround is to manually specify which backends you want when building matplotlib. You'll need to download the source (e.g. tarball) and build things manually (or use something pre-built with a gui backend like anaconda/canopy). To specify the backends, cp `setup.cfg.template` to `setup.cfg` and edit it accordingly (the file itself has extensive documentation). Also, if it can't build the gtk backend, you'll get more informative errors, instead of just skipping it. – Joe Kington Nov 08 '14 at 17:46
  • @JoeKington forcing gtk backend didn't work, but I managed to get PyQt4 installed site-wide, and then forcing matplotlib to install qt4agg backend worked! I'm going to figure out the minimal set of things I had to do and put an answer here. Thanks guys! – Scott Nov 08 '14 at 18:35

1 Answers1

3

I got it working on Ubuntu 14.04.1 Server, but it was painful! The tricky part is definitely virtualenv. I finally had luck using the Qt4 backend, which I was only able to install via the Ubuntu package and then had to symlink it into my virtualenv. So here's the step-by-step process...

First install the pre-reqs and hack PyQt4 into your virtualenv:

$ sudo apt-get install xauth x11-apps python-qt4 
$ ln -s /usr/lib/python2.7/dist-packages/PyQt4 /path/to/myvenv/lib/python2.7/PyQt4

Now manually download and install SIP (http://www.riverbankcomputing.com/software/sip/intro) with your venv activated, as follows:

$ tar xzf sip-4.16.4.tar.gz
$ cd sip-4.16.4
$ python configure.py
$ make
$ sudo make install

Next, download matplotlib source tarball and modify the setup configuration to force it to install Qt4 backend:

$ tar xzf matplotlib-1.4.2.tar.gz
$ cp matplotlib-1.4.2/setup.cfg.template matplotlib-1.4.2/setup.cfg

Now edit setup.cfg near line 68 to read:

qt4agg = True 

Matplotlib will now install cleanly in your venv:

$ pip install -e matplotlib-1.4.2/

Now you can SSH using the -X flag and plots will load remotely!

Scott
  • 2,764
  • 1
  • 16
  • 16