Matplotlib: display plot on a remote machine
Solution 1
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
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.
Solution 2
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.
Solution 3
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
Solution 4
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()
Solution 5
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.
Comments
-
Mermoz almost 2 years
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? -
zanbri over 8 yearsfor me it was /etc/ssh/sshd_config
-
sudo almost 7 yearsIt'll make you instal
pygtk
to do that, and it seems it's either impossible or a total pain to do that with Python 3. -
Bruno Feroleto almost 7 yearsThis definitely depend on your platform and package manager. On macOS, MacPorts has a py27-pygtk package that should make the install smooth for Python 2.
-
epokhe over 6 years@sudo I checked all the supported back-ends. In my case,
TkAgg
andGTK3Cairo
back-ends worked for Python 3.TkAgg
seems to be faster. -
wronk over 6 yearsIf you edit
/etc/ssh/sshd_config
, then make sure to restart the sshd and/or ssh service. Stupid simple way is to restart your computer, but (at least on linux) you can also restart the service using something likesudo service sshd restart
as described here: cyberciti.biz/faq/howto-start-stop-ssh-server -
Morse almost 6 yearsWelcome to Stack Overflow.While adding link may answer the question, it is better to include the essential parts of the link here and provide the link for reference.
-
Mike O'Connor over 5 yearsThere is additional helpful advice on this topic from Gilles, here.
-
Thomas Ahle about 4 yearsI had lots of trouble with both gtk and qt, but this worked great!
-
mostsquares about 4 yearsIf speed is a concern I recommend
-C
flag to SSH -
Cleb almost 4 yearsThat gives me:
TclError: no display name and no $DISPLAY environment variable
. Any ideas? -
Mauricio Arboleda almost 4 yearsDefinitely it solved my problem using python 3.6. Unfortunately displaying the images works really slow, any ideas of how improve the speed. I used ssh -Y -C to access to my remote machine.
-
vanPelt2 almost 4 yearsThis isn't really a direct answer to the original question, but it's a hugely advantageous workaround. If you save figures on the remote machine, you can even open them in the notebook viewer! imo it's even better with jupyter lab.