我在服务器上运行一个简单的python脚本:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(60)
y = np.random.randn(60)
plt.scatter(x, y, s=20)
out_png = 'path/to/store/out_file.png'
plt.savefig(out_png, dpi=150)
我尝试在这个安装了matplotlib 1.5.1的服务器上使用python example.py命令,它失败了,错误如下:
Traceback (most recent call last):
File "example.py", line 7, in <module>
plt.scatter(x, y, s=20)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3241, in scatter
ax = gca()
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 928, in gca
return gcf().gca(**kwargs)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 578, in gcf
return figure()
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1810, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
这里发生了什么?
Matplotlib默认选择Xwindows后端。
您需要设置matplotlib不使用Xwindows后端。
将这段代码添加到脚本的开头(在导入pyplot之前),然后重试:
import matplotlib
matplotlib.use('Agg')
或添加到.config/matplotlib/matplotlibrc line backend: Agg以使用非交互式后端。
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc
或者连接服务器时使用ssh -X remoteMachine命令使用Xwindows。
你也可以尝试导出display: export display =mymachine.com:0.0。
欲了解更多信息:https://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
虽然存在解决方案用途('Agg'),但可能有人对解决x11感兴趣。
假设我们有一个用户ml,在ubuntu20上使用python3…
首先,以ml的身份登录,并编写ml.py
$ cat ml.py
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(60)
y = np.random.randn(60)
plt.scatter(x, y, s=20)
out_png = 'out_file.png'
plt.savefig(out_png, dpi=150)
在运行文件之前,我们应该设置x11,
$ whoami
ml
$ touch ~/.Xauthority
$ ls .Xauthority
.Xauthority
# only this one key is needed for X11 over SSH
xauth generate :14 . trusted
$ echo $DISPLAY
localhost:14.0
$ xauth list
ml/unix:14 MIT-MAGIC-COOKIE-1 b6a2833785df5e75e2c8bc34045a55a6
运行……
$ python3 ml.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-rk288ipf because the default path (/home/ml/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
Matplotlib is building the font cache; this may take a moment.
qt.qpa.xcb: X server does not support XInput 2
failed to get the current screen resources
更新OpenGL和x11-apps,重新运行
$ python3 ml.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-87at6bw8 because the default path (/home/zzx/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
qt.qpa.xcb: X server does not support XInput 2
failed to get the current screen resources
似乎我们不能得到out_file.png,但是…
$ export QT_DEBUG_PLUGINS=1
然后重新运行文件,我们得到图像,:)