使用 matplotlib 绘制图形,使用 X11 和 OS X 通过 ssh

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

总结:当本地计算机 运行ning OS X 时,如何通过 SSH 将图形绘制到远程计算机?

我有计算机 A,我正在尝试使用 matplotlib 在计算机 B 上绘图。我遇到的问题是,当我使用 matplotlib 时,绘图仅显示在计算机 A 上。我进入 VNC 并看到它们弹出。我可以通过 ssh -X/-Y 进入计算机 A 和 运行 xcalc,它会显示在计算机 B 上。我可以将计算机 B 连接到第三台计算机,运行ning Red Hat,并且会显示绘图在计算机 B 上。我确信问题不是计算机 B。 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.

如前所述,我在installing backends for matplotlib on Computer A方面尝试了很多解决方案。我已经尝试了所有 macports 庄园和自制软件以及 pip 组合。我很确定混合这么多包处理程序是个坏主意,但这么多解决方案似乎是 "sudo ***** install package-name".

为了测试 matplotlib 是否在做我想做的事情,我使用了以下 python 片段:

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 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. 我还没有尝试以其他方式安装 GTK。我必须从源代码安装它吗?有人成功了吗?

GTK 错误:

ImportError: No module named _backend_gdk

Backends MacOS、TkAgg、qt5agg 都能正常工作,但数字显示在计算机 A 上。我必须安装 pyqt5。如果我没有通过 VNC 连接,那么 python 将抛出一个关于无显示的错误。这三个都给出相同的错误:

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.

详情:

计算机 A 运行s OS X 10.11 计算机 B 运行s OS X 10.12

编辑: 我从源代码(连同 QT)安装了 PyQt4,但没有帮助。我明确下载并安装了 X11 版本。我设置了matplotlib.use('qt4agg'),但数字仍然出现在计算机A上。也许是我为"macosx"安装了QT?我不知道

This ended up leading me to the right answer. 问题是我无法安装使用 X11 的后端。事实证明 macports 有一个使用 x11 的 tk 版本。我认为这实际上是使用 macports 安装 python 时的默认设置。

我从 OS X 服务器(计算机 A)获取 matplotlib 在 ssh/X11 上绘图的步骤是:

1) 卸载之前安装的matplotlib和tk的macports:

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

2) Re-install matplotlib 使用 X11

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

可能不需要“-cairo”。 tk 没有标志,因为 x11 是默认值。

3) 测试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)

这将在计算机 B 上打开一个图形。

一些注意事项:

1) 确保您的 $PYTHONPATH 环境变量中没有任何内容指向由另一个安装程序执行的 python 安装,例如自制软件或系统 python。这可能会导致加载错误的 matplotlib 安装。

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

3) 正如我原来的 post、Homebrew does not support X11 anymore for GTK. There is, apparently a way to get homebrew to use tk with X11 中所述,但我还没有弄清楚如何让它发挥作用。