Python Matplotlib:在远程机器上显示绘图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3453188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:10:09  来源:igfitidea点击:

Matplotlib: display plot on a remote machine

pythonsshmatplotlib

提问by Mermoz

I have a python code doing some calculation on a remote machine, named A. I connect on A via sshfrom a machine named B. Is there a way to display the figure on machine B?

我有一个 python 代码在名为 A 的远程机器上进行一些计算。我通过ssh名为 B 的机器连接到 A。有没有办法在机器 B 上显示图形?

采纳答案by Eric O Lebigot

If you use matplotlib on Mac OS X on the remote machine (B), you must first make sure that you use one of the X11-based display back-ends, since the native Mac OS X back-end cannot export its plots to another display. Selecting a back-end can be achieved with

如果在远程计算机 (B) 上的 Mac OS X 上使用 matplotlib,则必须首先确保使用基于 X11 的显示后端之一,因为本机 Mac OS X 后端无法将其绘图导出到另一个展示。可以通过以下方式选择后端

import matplotlib
matplotlib.use('GTK')  # Or any other X11 back-end

The list of supported back-ends can be obtained by giving use()an incorrect back-end name: matplotlib then prints an error message listing the possible back-ends.

可以通过提供use()不正确的后端名称来获得支持的后端列表:matplotlib 然后打印一条错误消息,列出可能的后端。

ssh X11 forwarding can then be used to display matplotlib plots.

然后可以使用 ssh X11 转发来显示 matplotlib 图。

回答by David Z

Sure, you can enable X11 forwarding. Usually this is done by passing the -Xor -Yoption to sshwhen you connect to the remote computer

当然,您可以启用 X11 转发。通常这是通过在连接到远程计算机时传递-X-Y选项来完成的ssh

ssh -X computerA

Note that the SSH daemon on computer A will also have to be configured to enable X11 forwarding. This is done by putting

请注意,计算机 A 上的 SSH 守护程序也必须配置为启用 X11 转发。这是通过把

X11Forwarding yes

in computer A's sshd_configconfiguration file.

在计算机 A 的sshd_config配置文件中。

If computer A's SSH daemon does not have X11 forwarding enabled, you can always have Python write the result of the calculation to a text file, download it to computer B, and use Matplotlib locally.

如果计算机 A 的 SSH 守护进程没有启用 X11 转发,您始终可以让 Python 将计算结果写入文本文件,下载到计算机 B,并在本地使用 Matplotlib。

回答by ValAyal

The following worked for me using Mac OS X on the local machine (machine B) and ubuntu on the remote (machine A).

以下对我使用本地机器(机器 B)上的 Mac OS X 和远程机器(机器 A)上的 ubuntu 对我有用。

You need X11 server installed on your local machine to do this.

您需要在本地计算机上安装 X11 服务器才能执行此操作。

If you're running a recent version of Mac OSX (OS X Mountain Lion or newer), it would NOT have come with X11 pre-installed (see http://support.apple.com/kb/ht5293). Check if you have X11 by opening up Mac terminal, and run command xterm. If an X11 window opens up, you're all set. If it says command not found, then go to http://xquartz.macosforge.org/landing/and install X11 server. Then logout and log back in to your mac.

如果您运行的是最新版本的 Mac OSX(OS X Mountain Lion 或更新版本),则它不会预装 X11(请参阅http://support.apple.com/kb/ht5293)。打开 Mac 终端检查您是否有 X11,然后运行命令xterm。如果 X11 窗口打开,则一切就绪。如果它说找不到命令,则转到http://xquartz.macosforge.org/landing/并安装 X11 服务器。然后注销并重新登录到您的 Mac。

After you log back in, try to run xtermcommand again. It should open up X11 window. At this point your $DISPLAY variable should also be set correctly. If it's not set, make sure you've logged in/out since installing X11 from XQuartz.

重新登录后,xterm再次尝试运行命令。它应该打开 X11 窗口。此时,您的 $DISPLAY 变量也应该正确设置。如果未设置,请确保自 XQuartz 安装 X11 以来您已登录/注销。

echo $DISPLAY
/tmp/launch-I9I3aI/org.macosforge.xquartz:0

Then from your local machine, use ssh -X to remote into remote machine A:

然后从您的本地机器,使用 ssh -X 远程进入远程机器 A:

ssh -X user@machineA

Then on the remote machine:

然后在远程机器上:

python
>>> import matplotlib
>>> matplotlib.use('GTKAgg')  #I had to use GTKAgg for this to work, GTK threw errors
>>> import matplotlib.pyplot as plt  #...  and now do whatever you need...

Make sure you call matplotlib.useBEFORE importing anything else from matplotlib(e.g. matplotlib.pyplot)

确保matplotlib.use在从matplotlib(例如matplotlib.pyplot)导入任何其他内容之前调用

Other useful troubleshooting tips on using ssh -X : http://oroborosx.sourceforge.net/remotex.html#usessh

关于使用 ssh -X 的其他有用的故障排除技巧:http: //oroborosx.sourceforge.net/remotex.html#usessh

回答by wordsforthewise

GTK seems impossible to get working on Ubuntu with Python3. Instead, I used tkagg (from thisanswer):

GTK 似乎不可能使用 Python3 在 Ubuntu 上工作。相反,我使用了 tkagg(来自这个答案):

import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt

Test that it's working with this:

测试它是否与此一起工作:

import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.show()

回答by Samaksh Goyal

if that doesn't work you could also try:

如果这不起作用,您也可以尝试:

import matplotlib.pyplot as plt
plt.switch_backend('agg')

or

或者

import matplotlib.pyplot as plt
plt.switch_backend('TkAgg')

this seemed to work for me

这似乎对我有用

Yet, if you are trying to get a GUI working I suggest you look at this link: http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/

但是,如果您想让 GUI 工作,我建议您查看此链接:http: //fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/

回答by Farhana Liza

I have used IPython to solve the related problem. The steps are as follows:

我已经使用 IPython 来解决相关问题。步骤如下:

Step 1:Install IPython and Jupyter in the remote machine (A) locally (assuming no root privilege) using the following commands:

步骤 1:使用以下命令在本地远程机器 (A) 中安装 IPython 和 Jupyter(假设没有 root 权限):

pip install --user ipython
pip install --user jupyter 

Update matplotlib:

更新 matplotlib:

pip install --user -U matplotlib

Step 2:

第2步:

Run Jupyter with no browser from the code directory in the remote machine (A):

从远程机器 (A) 中的代码目录运行没有浏览器的 Jupyter:

cd PATH/TO/THE/CODE
jupyter notebook --no-browser --port=8080

After this command, a URL will be given something similar to below:

执行此命令后,将给出类似于以下内容的 URL:

http://localhost:8080/?token=5528ab1eeb3f621b90b63420f8bbfc510edf71f21437c4e2

http://localhost:8080/?token=5528ab1eeb3f621b90b63420f8bbfc510edf71f21437c4e2

Step 3:

第 3 步:

Now open another terminal in the local machine (B) and connect to the remote machine (A) using ssh:

现在在本地机器 (B) 中打开另一个终端并使用 ssh 连接到远程机器 (A):

ssh -N -L 8080:localhost:8080 [email protected]

The port number has to be same in step 2and step 3. In this example, the port number is 8080.

步骤 2步骤 3 中的端口号必须相同。在本例中,端口号为 8080。

Step 4:

第四步:

Copy and paste the URL in the step 3to a browser in your local machine (B).

步骤 3 中的 URL 复制并粘贴到本地计算机 (B) 中的浏览器。

Now, the notebook in the remote machine can be used through the browser and plot can be generated using the data in the remote machine.

现在,可以通过浏览器使用远程机器中的笔记本,并且可以使用远程机器中的数据生成绘图。

回答by amazia

export MPLBACKEND="agg"this worked for me. obviously you can set it via code as well.

export MPLBACKEND="agg"这对我有用。显然你也可以通过代码设置它。