Open a Matplotlib figure through SSH tunnel of vscode
I managed to solve the problem by running a parallel Putty ssh connection with X11 forwarding enabled and by writing export DISPLAY=localhost:10.0
in the terminal of vscode before launching a python script.
However, I have safety concerns and I wonder why I have to manually do these commands in order for it to work... Any insight would be much appreciated !
I found a slight variation of a previous answer to work very well.
Save the plot as .png
instead of plotting it.
plt.savefig("dummy_name.png")
The previous answer then suggests moving the file through scp
. I instead suggest to open dummy_name.png
with Visual Code, no need to write commands and it automatically refreshes the image whenever you plot something else. I tested this from USA with SSH into a server in Europe, the images refreshed in less than half a second.
For an easy configuration, you have 2 options :
- Plot without showing and transfer picture file through SSH
This option consists in replacing plt.show()
with
plt.savefig('foo.png')plt.savefig('foo.pdf')
More information on saving matplotlib
plots
After that, you can transfer this figure with scp
scp remote_username@10.10.0.2:/path/to/foo.png /local/directory
- Use Jupyter Notebook
You can easily plot into a Jupyter Notebook. Here is a tutorial on how to setup a Raspberry Pi through ssh
to create a remote server for Jupyter Notebook.
On Jupyter Notebook, you need to add on the first line of your notebook, before importing matplotlib
, to plot your image after cell.
%matplotlib inline