2

vscode was installed in my windows computer, I use ssh remote service and when I tried to plot a figure, the figure just did not show up. e.g., the code as follows

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y)
plt.show()

when I run the code in the remote ssh, the figure didn't show up. (ps, the code works in my local computer)

2
  • 1
    How did you call SSH? What is the matplotlib backend on the remote machine? I don't think that the question has anything to do with VSCode. Nov 27, 2019 at 6:02
  • i have tried ssh by using ss username@ip and ssh -XY username@ip, but both of them don't work. Also, I used xshell, which need xmanager to show figures, it works in remote machine.
    – dl wu
    Nov 28, 2019 at 2:43

3 Answers 3

3

Yes its completely possible, but it requires the x11 Forwarding option. In your ssh config file, add "ForwardX11 Yes"

Host myHost
  Hostname 192.168.1.1
  ForwardX11 Yes

Now the tricky part: the remote computer has to allow the forwarding (usually disabled by default). On the remote host, go to the sshd_config file (usual location is /etc/ssh/sshd_config) And make sure that the option

X11Forwarding yes

is present and that its NOT commented out.

Now whenever you ssh into that host, you should be able to run any graphical application from your windows computer

4
  • thanks for the answer, but it still doesn't work for me even though i tried "ForwardAgent yes, ForwardX11Trusted yes" too, and "ssh ... -XY" as well.
    – dl wu
    Nov 27, 2019 at 8:17
  • Do you receive any error message when running "ssh -X host"? You should see a message like "X11 forwarding request failed on channel 0" if the X forwarding failed
    – Cydouzo
    Nov 27, 2019 at 8:39
  • thanks for a quick reply, I didn't receive any error message,.
    – dl wu
    Nov 27, 2019 at 10:33
  • For Python code, I found an alternative way to show figures by adding "# %%" at the top of the code, and then one can see "Run cell" button, click it, the figure will show up at the right side of the window. see the link for those who need: stackoverflow.com/questions/49992300/…
    – dl wu
    Nov 27, 2019 at 10:44
1

I struggled with the same problem so I created a library to solve this here https://pypi.org/project/remote-plot/.

It doesn't require X11 forwarding / having a display.

It uses the exact same API as matplotlib (and actually uses matplotlib to plot stuff), but it renders the plots in a web browser which you can view from your local machine.

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
0

Here are two options (which I think require the Jupyter extension to be installed remotely...someone correct me if I'm wrong and I'll edit this answer).

Option 1: Display figures generated by a .py file

  • Using VScode with the Remote - SSH extension to connect to a remote server, you can right-click in the file text area and select Run Current File in Interactive Window. This will show plots in what looks to me like a iPython or jupyter notebook type window.

For example, if you have basic_plot.py with the following:

import matplotlib.pyplot as plt
x = [i for i in range(100)]
y = [10*i for i in x]
plt.plot(x,y, '-x')

With basic_plot.py open in VScode for editing, right-click & select Run Current File in Interactive Window. This will open a window like below enter image description here

Option 2: Display pyplot figures by writing code in iypthon/jupyter notebook style interactive window

  • Open the VScode command palatte (either via the View menu or `Ctrl+Shift+P').
  • Type "Create Interactive Window" in the command palette.
  • Select "Jupyter: Create Interactive Window".
  • An interactive window (like in the pic from option 1 above) will appear where you can write and execute python code like in ipython or jupyter notebook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.