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

标签 python matplotlib cron

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

追溯(最近的调用最后):

File "/home/ubuntu/python/spread.py", line 154, in <module>
    plot_spread(lat, lon, vals, mean, maxs, mins, stdp, stdm, ens_members)
  
File "/home/ubuntu/python/spread.py", line 81, in plot_spread
    plt.fill_between(x, maxs, stdp, color='r', alpha=0.2)
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 2785, in fill_between
    ax = gca()
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 928, in gca
    return gcf().gca(**kwargs)
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 578, in gcf
    return figure()
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 527, in figure
    **kwargs)
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 46, in new_figure_manager
    return new_figure_manager_given_figure(num, thisFig)
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 53, in new_figure_manager_given_figure
    canvas = FigureCanvasQTAgg(figure)
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 76, in __init__
    FigureCanvasQT.__init__(self, figure)
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4.py", line 68, in __init__
    _create_qApp()
  
File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')

RuntimeError: Invalid DISPLAY variable

最佳答案

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

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

这些链接也可能有帮助:

Generating a PNG with matplotlib when DISPLAY is undefined

Generating matplotlib graphs without a running X server

希望对您有所帮助!

关于python - 使用 cron 运行 python 脚本,matplotlib 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34801219/

相关文章:

python - 允许将 %d.%m.%Y 格式字符串传递给模型的 DateField 的最佳方法?

python - 嵌入式python模块导入错误

python - 发现对 SQLAlchemy 对象的引用

python - 如何在python中将列表转换为带引号的字符串

python - matplotlib 不在散点图中显示图例

python - 如何使我的 matplotlib rc 模块成为所有 python 文件的默认参数?

python - 在matplotlib中制作更长的子图刻度线?

php - 在 cron 作业中执行 PHP 脚本

linux - Bash 脚本写入而不覆盖?

php - "(include_path=' 是什么意思。 :/usr/share/pear:/usr/share/php')"?