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.