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

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

其他回答

另一种解决方案是安装Xvfb,并将显示导出到它。 即:

disp=:8
screen=0
geom=640x480x24
exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &

Then

$ export DISPLAY=:8

美元。/ example.py

虽然存在解决方案用途('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窗口系统中的一个程序,它运行在本地机器(即用户直接使用的计算机)上,并处理对这些计算机上的显卡、显示屏和输入设备(通常是键盘和鼠标)的所有访问。

更多信息请点击这里。

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

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

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为例。