Matplotlib: 远程机器上的显示图

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

107805 次浏览

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.

如果在远程计算机(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.

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

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

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()

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/

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 user_id@remote.host

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.

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

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.

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

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()

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