Matplotlib:在远程计算机上显示绘图

发布于 2024-09-13 22:43:18 字数 95 浏览 1 评论 0 原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

尝蛊 2024-09-20 22:43:18

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

ssh -X computerA

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

X11Forwarding yes

计算机 A 的 sshd_config 配置文件来完成的。

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

Sure, you can enable X11 forwarding. Usually this is done by passing the -X or -Y option to ssh when you connect to the remote computer

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

X11Forwarding yes

in computer A's sshd_config configuration file.

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.

秋凉 2024-09-20 22:43:18

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

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

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

然后可以使用 ssh X11 转发来显示 matplotlib 绘图(请参阅 David Z 的回答)。

If you use matplotlib on Mac OS X on the remote machine (A), 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

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.

ssh X11 forwarding can then be used to display matplotlib plots (see David Z's answer).

山色无中 2024-09-20 22:43:18

以下内容对我在本地计算机(计算机 B)上使用 Mac OS X 和在远程计算机(计算机 A)上使用 ubuntu 有效。

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

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

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

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

然后从本地计算机上,使用 ssh -X 远程访问远程计算机 A:

ssh -X user@machineA

然后在远程计算机上:

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...

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

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

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

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

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.

After you log back in, try to run xterm command 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.

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 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.use BEFORE importing anything else from matplotlib (e.g. matplotlib.pyplot)

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

ぶ宁プ宁ぶ 2024-09-20 22:43:18

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

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

测试它是否可以使用:

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

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

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()
囚你心 2024-09-20 22:43:18

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

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

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

更新 matplotlib:

pip install --user -U matplotlib

< strong>第 2 步:

从远程计算机的代码目录中不使用浏览器运行 Jupyter (A):

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

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

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

第 3 步:

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

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

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

步骤 4:

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

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

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

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

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

Update matplotlib:

pip install --user -U matplotlib

Step 2:

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

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

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

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

Step 3:

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

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

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

Step 4:

Copy and paste the URL in the step 3 to a browser in your local machine (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.

一人独醉 2024-09-20 22:43:18

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

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

暗恋未遂 2024-09-20 22:43:18

您可以使用我创建的库来解决这个问题 https://pypi.org/project/remote-情节/

它使用与 matplotlib 完全相同的 API,但它在 Web 浏览器中渲染绘图,您可以从您的计算机上查看 B.

安装方式:

pip install remote_plot

然后像这样在 python 中运行:

from remote_plot import plt
plt.plot([1, 2, 3], [4, 5, 6])

默认情况下,它会在端口 8000 上打开渲染,但您可以很容易修改这个。如果您通过 ssh 连接,请不要忘记通过将以下标志添加到 ssh 命令来转发端口:

ssh YOUR_USER_NAME@YOUR_MACHINE_IP -L 8000:localhost:8000

You can use the library I created to solve this problem https://pypi.org/project/remote-plot/.

It uses the exact same API as matplotlib, but it renders the plots in a web browser which you can view from your machine B.

Install by:

pip install remote_plot

And then run in python like this:

from remote_plot import plt
plt.plot([1, 2, 3], [4, 5, 6])

By default it opens the rendering on port 8000 but you can modify this easily. If you are connecting via ssh, don't forget to forward the port by adding the following flag to your ssh command:

ssh YOUR_USER_NAME@YOUR_MACHINE_IP -L 8000:localhost:8000
惯饮孤独 2024-09-20 22:43:18

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

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

或者

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

这似乎对我有用

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

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/

戒ㄋ 2024-09-20 22:43:18

只是想补充一下 - 如果您在 Windows 上作为本地计算机,请确保您已设置 Xming(X Windows 服务器)和 Putty,以便您可以看到远程 Linux 图形应用程序。

我按照这里的说明进行操作: http ://laptops.eng.uci.edu/software-installation/using-linux/how-to-configure-xming-putty 来执行此操作。它还设置您的显示环境和变量,以便您在使用 tkagg 作为后端时不会出现错误。

Just wanted to add - if you're on Windows as the local machine, make sure you've set up Xming (an X Windows server) and Putty so you can see the remote Linux graphical applications.

I followed the instructions from here: http://laptops.eng.uci.edu/software-installation/using-linux/how-to-configure-xming-putty to do this. It also sets your display environment and variable so you don't get an error when using tkagg as the backend.

剩余の解释 2024-09-20 22:43:18

一个临时解决方案(我实际上正在搜索如何执行此操作)是使用 VSCode 远程连接,保存绘图,然后只需单击 VScode 中的文件即可显示它。
这并不理想,但是...

A temporary solution (I am actually searching how to do this) is to use VSCode to connect remotely, save the plot and just click the file in VScode which will display it.
It is not ideal but...

初相遇 2024-09-20 22:43:18

如果您使用 Mac 作为客户端计算机,请尝试此操作。

您基本上需要确保两件事正常工作。

  • 使用 GTK 或 Cairo 作为 matplotlib 的后端。
  • 转发显示。

使用 GTK 或 Cairo 作为 matplotlib 的后端

如果您使用 python3,则必须安装 cairocffi

pip install cairocffi

然后使用 GTK3Agg 作为 matplotlib 的后端。

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

有关更多详细信息,请参阅 matplotlib 文档中的以下描述。

GTK2 和 GTK3 都对 PyCairo 具有隐式依赖关系,无论
使用的特定 Matplotlib 后端。不幸的是最新版本
PyCairo for Python3 没有实现所需的 Python 包装器
用于 GTK3Agg 后端。 Cairocffi 可以用作替代品
实现正确的包装器。

转发显示

  1. 安装启动最新版本的 XQuartz。
  2. 使用 ssh -X 连接到远程服务器。例如)ssh 用户名@ipaddress -X

If you're using a mac as a client machine, try this.

You basically need to make sure two things are working properly.

  • Using GTK or Cairo as matplotlib's backend.
  • Forwarding the display.

Using GTK or Cairo as matplotlib's backend

If you're using python3, you must install cairocffi.

pip install cairocffi

Then use the GTK3Agg as a backend of matplotlib.

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

See following description from matplotlib document for more detail.

Both GTK2 and GTK3 have implicit dependencies on PyCairo regardless of
the specific Matplotlib backend used. Unfortunatly the latest release
of PyCairo for Python3 does not implement the Python wrappers needed
for the GTK3Agg backend. Cairocffi can be used as a replacement which
implements the correct wrapper.

Forwarding the display

  1. Install launch the latest version of XQuartz.
  2. Connect to the remote server using ssh -X. ex) ssh username@ipaddress -X
給妳壹絲溫柔 2024-09-20 22:43:18

这就是我在 MacOS 和 Linux 远程计算机上所做的。

  1. 在 ~/.ssh/config 中我添加了以下内容。我添加此内容是因为您的计算机可能无法正确获取 xauth 位置。
    XAuthLocation /opt/X11/bin/xauth
  1. ssh -Y machine_name
  2. 运行以下虚拟程序:
    import matplotlib.pyplot as plt
    x = [1,2,3]
    y = [2,4,1]
    plt.plot(x, y)
    plt.show()

This is what I did with MacOS and a Linux remote machine.

  1. In ~/.ssh/config I added the following. I add this since it's possible that your machine might not be taking the xauth location properly.
    XAuthLocation /opt/X11/bin/xauth
  1. ssh -Y machine_name
  2. Ran the following dummy program:
    import matplotlib.pyplot as plt
    x = [1,2,3]
    y = [2,4,1]
    plt.plot(x, y)
    plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文