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

这里发生了什么?


当前回答

您可以通过在.py脚本的VERY开头添加这两行来解决这个问题。

import matplotlib
matplotlib.use('Agg')

PS:如果没有在源代码的最开始添加这两行,错误仍然会存在。

其他回答

我在使用Xshell连接Linux服务器时也遇到了这个问题。

在寻找方法后,我找到了用matplotlib解决图像imshow问题的Xming + Xshell。

如果上述解决方案不能解决您的问题,请在使用Xshell的情况下尝试下载Xming。然后在Xshell中设置属性,SSH->tunnel->X11transfer->choose X DISPLAY localhost:0.0

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

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

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

export DISPLAY=:0.0

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

为了增加答案,我在所需脚本的开头使用了这个。所以它可以在不同的环境下顺利运行。

import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
    print('no display found. Using non-interactive Agg backend')
    mpl.use('Agg')
import matplotlib.pyplot as plt

因为我不希望它总是使用“Agg”后端,只有当它将通过Travis CI为例。

尝试使用python3,每个问题都解决了