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

其他回答

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

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

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

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

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

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

导出 MPLBACKEND=Agg

这就解决了这个问题。

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

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

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

为了查看远程机器上显示的图像、图形和任何内容,你需要像这样连接到它:

ssh -X user@hostname

这样可以启用对X服务器的访问。X服务器是X窗口系统中的一个程序,它运行在本地机器(即用户直接使用的计算机)上,并处理对这些计算机上的显卡、显示屏和输入设备(通常是键盘和鼠标)的所有访问。

更多信息请点击这里。

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

import matplotlib
matplotlib.use('Agg')

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