Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a Matplotlib figure through SSH tunnel of vscode

I am setting up a remote workstation to run machine learning related python code from my laptop on another computer that includes a GPU.

I use the SSH remote feature of vscode to remotely run and debug my code and I am very happy with the interface. However, I am not able to generate figures coming from the "matplotlib" library.

I tried to modify some matlplotlib options, but it was unsucessful :

import matplotlib
matplotlib.use('Agg')

As instructed in : _tkinter.TclError: no display name and no $DISPLAY environment variable

In short my problem emerges with the following example code :

import numpy as np
from matplotlib import pyplot as plt 

x = np.linspace(0,1,101)
y = x ** 2

plt.close()
plt.figure()
plt.plot(x,y)
plt.show()
~/vscode$ cd /home/*/vscode ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /usr/bin/python3 /home/*/.vscode-server/extensions/ms-python.python-2019.6.24221/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 39903 /home/*/vscode/test_plot.py 
Traceback (most recent call last):
  File "/home/*/.vscode-server/extensions/ms-python.python-2019.6.24221/pythonFiles/ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "/home/*/.vscode-server/extensions/ms-python.python-2019.6.24221/pythonFiles/lib/python/ptvsd/__main__.py", line 434, in main
    run()
  File "/home/*/.vscode-server/extensions/ms-python.python-2019.6.24221/pythonFiles/lib/python/ptvsd/__main__.py", line 312, in run_file
    runpy.run_path(target, run_name='__main__')
  File "/usr/lib/python3.6/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/usr/lib/python3.6/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/*/vscode/test_plot.py", line 8, in <module>
    plt.figure()
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 539, in figure
    **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 171, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 1049, in new_figure_manager_given_figure
    window = Tk.Tk(className="matplotlib")
  File "/usr/lib/python3.6/tkinter/__init__.py", line 2023, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

Thanks a lot for helping me !

like image 913
ju95ju Avatar asked Jul 22 '19 19:07

ju95ju


People also ask

Can Vscode connect to SSH?

The Visual Studio Code Remote - SSH extension allows you to open a remote folder on any remote machine, virtual machine, or container with a running SSH server and take full advantage of VS Code's feature set. Once connected to a server, you can interact with files and folders anywhere on the remote filesystem.

How do you use TensorBoard in Vscode?

To start a TensorBoard session, open the Command Palette (Ctrl+Shift+P) and search for the command Python: Launch TensorBoard. Afterwards, you'll be prompted to select the folder where your TensorBoard log files are located.


1 Answers

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.

like image 57
Alexander Mathiasen Avatar answered Sep 27 '22 16:09

Alexander Mathiasen