简体   繁体   中英

Plotting figures using matplotlib, over ssh using X11 and OS X

Summary: How do I plot figures, over SSH, to a remote computer when the local computer is running OS X?

I have computer A and I am trying to use matplotlib to plot on computer B. The problem I am having is that when I use matplotlib the plots only display on Computer A. I VNC in and watch them pop up. I can ssh -X/-Y into Computer A and run xcalc and it will display on computer B. I can connect Computer B to a third computer, running Red Hat, and plots will display on Computer B. I am convinced it is not Computer B that is the problem. I believe my problem is the same as this problem: none of the package installers support X11 backends for matplotlib. I cannot comment so I'm stuck putting what I've tried in a new question. This is another description of the same problem with multiple solution attempts that do not work.

As mentioned, I have tried a lot of solutions in terms of installing backends for matplotlib on Computer A . I've tried all manor of macports and homebrew and pip combinations. I'm pretty sure it is a bad idea to mix so many package handlers, but so many solutions seem to be "sudo ***** install package-name".

To test whether matplotlib is doing what I want I use the following python snippet:

import matplotlib
matplotlib.use('gtk') # gtk is an example, I change it
import matplotlib.pyplot as plt
plt.ion()
plt.figure(1)
plt.plot([1]*10)

I can't recall all the things I have tried. Some things I have tried:

I tried using GTK and GTKCairo, which did not solve my problem because I cannot get GTK to work. Homebrew GTK does not support X11 anymore anyway, so even if it did install properly I don't think it will solve my problem. I have not yet tried to install GTK some other way. Would I have to install it from source? Has anyone got this working?

The GTK error:

ImportError: No module named _backend_gdk

Backends MacOS, TkAgg, qt5agg all work but figures display on Computer A. I had to install pyqt5. If I am not connected via VNC, then python will thrown an error about no displays. All three give the same error:

Feb 22 13:00:22  Python[57649] <Error>: Set a breakpoint at CGSLogError to catch errors as they are logged.
Feb 22 13:00:22  Python[57649] <Error>: This user is not allowed access to the window system right now.
_RegisterApplication(), FAILED TO establish the default connection to the WindowServer, _CGSDefaultConnection() is NULL.

This does not solve my problem. This doesn't either.

Details:

Computer A runs OS X 10.11 Computer B runs OS X 10.12

Edit: I installed PyQt4 from source (along with QT) and it didn't help. I explicitly downloaded and installed the X11 version. I set matplotlib.use('qt4agg') and figures still appear on Computer A. Perhaps it was my install of QT for "macosx"? I dunno

This ended up leading me to the right answer. The problem was I couldn't get a backend installed that used X11. It turns out macports has a tk version that uses x11. I think this is actually the default setting when installing python using macports.

The steps I took to get matplotlib plotting over ssh/X11 from an OS X server (Computer A) were:

1) Uninstall the previous macports install of matplotlib and tk:

sudo port uninstall py27-matplotlib
sudo port uninstall py27-tkinter
sudo port uninstall tk

2) Re-install matplotlib to use X11

sudo port install tk
sudo port install py27-tkinter
sudo port install py27-matplotlib -cairo +tkinter

The "-cairo" may not be necessary. tk has no flags because x11 is default.

3) Test python+matplotlib:

import matplotlib
matplotlib.use('tkagg') # set the backend to tk, using agg renderer
import matplotlib.pyplot as plt
plt.ion()
plt.figure(1)
plt.plot([1]*10)

This opens a figure on Computer B.

Some notes:

1) Make sure there isn't anything in your $PYTHONPATH environment variable that points to a python installation performed by another installer, like homebrew or the system python. It might cause the wrong install of matplotlib to load.

2) You can use a matplotlibrc configuration file to avoid having to re-specify the backend every time you open matplotlib.

3) As noted in my original post, Homebrew does not support X11 anymore for GTK. There is, apparently a way to get homebrew to use tk with X11 , but I haven't figured out how to make it work yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM