我正在设置一个远程工作站,以便在另一台包含 GPU 的计算机上从我的笔记本电脑运行与机器学习相关的 python 代码。
我使用 vscode 的 SSH 远程功能来远程运行和调试我的代码,我对这个界面非常满意。但是,我无法生成来自“matplotlib”库的图形。
我尝试修改一些 matplotlib 选项,但没有成功:
import matplotlib
matplotlib.use('Agg')
按照指示:_tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量 https://stackoverflow.com/questions/37604289/tkinter-tclerror-no-display-name-and-no-display-environment-variable/43592515
简而言之,我的问题出现在以下示例代码中:
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
非常感谢您对我的帮助!