使用 cron 运行 python 脚本,matplotlib 错误

use*_*093 3 python cron matplotlib

我正在尝试使用 cron 运行 python 脚本。该脚本从命令行运行时没有问题,但从 cron 运行时,matplotlib 出现问题。错误如下。

回溯(最近一次调用最后一次):

文件“/home/ubuntu/python/spread.py”,第 154 行,在 plot_spread(lat, lon, vals, mean, maxs, mins, stdp, stdm, ens_members)

文件“/home/ubuntu/python/spread.py”,第 81 行,在 plot_spread plt.fill_between(x, maxs, stdp, color='r', alpha=0.2)

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”,第2785行,在fill_between ax = gca()

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”,第928行,gca返回gcf()。 gca(**kwargs)

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”,第578行,gcf返回图()

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py”,第527行,图**kwargs)

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py”,第46行,new_figure_manager return new_figure_figurer数量,这个图)

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py”,第53行,在new_figure_manager_givenQfigureTA canvas = (数字)

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py”,第76行,在init FigureCanvasQT中。初始化(自我,数字)

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4.py”,第68行,在init _create_qApp()

文件“/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt5.py”,第138行,在_create_qApp引发运行时错误( '无效的 DISPLAY 变量')

运行时错误:无效的 DISPLAY 变量

Tam*_*lan 5

当我尝试使用 cron 在 python 中导入 matplotlib.pyplot 时,我遇到了类似的错误。您可以强制 matplotlib 不使用任何 Xwindows 后端。做这个:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)

这些链接可能也有帮助:

未定义 DISPLAY 时使用 matplotlib 生成 PNG

在没有运行 X 服务器的情况下生成 matplotlib 图

希望这是有帮助的!