我在服务器上运行一个简单的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

这里发生了什么?


当前回答

虽然存在解决方案用途('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

然后重新运行文件,我们得到图像,:)

其他回答

我在Colab上遇到了这个问题,下面的代码行解决了它。 把它放在笔记本的开头。 参考

### CREATE VIRTUAL DISPLAY ###
!apt-get install -y xvfb # Install X Virtual Frame Buffer
import os
os.system('Xvfb :1 -screen 0 1600x1200x16  &')    # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
os.environ['DISPLAY']=':1.0'    # tell X clients to use our virtual DISPLAY :1.0.
%matplotlib inline
### INSTALL GHOSTSCRIPT (Required to display NLTK trees) ###
!apt install ghostscript python3-tk

虽然存在解决方案用途('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

然后重新运行文件,我们得到图像,:)

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

我想在这里补充一个答案,没有人明确地说明实现。

对于这种失败,这是一个很好的参考资源: https://matplotlib.org/faq/usage_faq.html

在我的例子中,使用matplotlib。Use没有起作用,因为它在某种程度上已经在其他地方设置了。然而,我能够通过定义一个环境变量来克服这个错误:

导出 MPLBACKEND=Agg

这就解决了这个问题。

我的错误是在CircleCI流中,这解决了失败的测试。一件奇怪的事情是,我的测试在使用pytest运行时会通过,但是在使用并行性和circleci测试拆分功能时会失败。但是,声明这个env变量解决了这个问题。

我试图在树莓派上远程运行一个简单的tkinter应用程序时也遇到了同样的问题。在我的例子中,我确实希望在pi显示器上显示tkinter GUI,但我希望能够从我的主机上通过SSH执行它。我也没有使用matplotlib,所以这不是我的问题的原因。我能够通过设置DISPLAY环境变量来解决这个问题,就像错误提示的命令一样:

export DISPLAY=:0.0

关于display环境变量的作用以及语法为何如此奇怪的很好的解释可以在这里找到:https://askubuntu.com/questions/432255/what-is-display-environment-variable