Python 当 DISPLAY 未定义时使用 Matplotlib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21321292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:34:58  来源:igfitidea点击:

Using Matplotlib when DISPLAY is undefined

pythonmatplotlib

提问by Chris Fonnesbeck

When using Matplotlib on a remote machine (e.g. on Travis CI), I run into frequent runtime errors related to the DISPLAY environmental variable not being set. As recommended, I have set the Aggbackend via matplotlib.useat the beginning of my test scripts, and have ensured there are no calls to show(). Nevertheless, I still get errors such the following:

在远程机器上(例如在 Travis CI 上)使用 Matplotlib 时,我经常遇到与未设置 DISPLAY 环境变量相关的运行时错误。按照建议,我在测试脚本的开头设置了Agg后端 via matplotlib.use,并确保没有调用show(). 尽管如此,我仍然收到如下错误:

Traceback (most recent call last):
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/travis/build/pymc-devs/pymc/pymc/tests/test_plots.py", line 36, in test_multichain_plots
    forestplot(ptrace, vars=['early_mean', 'late_mean'])
  File "/home/travis/build/pymc-devs/pymc/pymc/plots.py", line 325, in forestplot
    interval_plot = subplot(gs[0])
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 896, in subplot
    fig = gcf()
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 450, in gcf
    return figure()
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 31, in new_figure_manager
    return new_figure_manager_given_figure(num, thisFig)
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 38, in new_figure_manager_given_figure
    canvas = FigureCanvasQTAgg(figure)
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 70, in __init__
    FigureCanvasQT.__init__( self, figure )
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 207, in __init__
    _create_qApp()
  File "/home/travis/anaconda/envs/testenv/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 62, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

Are there other recommendations to averting this?

还有其他建议可以避免这种情况吗?

回答by ariddell

Using xvfbworks, see Travis's GUI & Headless browser testingdocumentation.

使用xvfb作品,请参阅 Travis 的GUI 和 Headless 浏览器测试文档。

p.s. The matplotlib.use('Agg')trick works for me.

ps这个matplotlib.use('Agg')技巧对我有用。