当 DISPLAY 未定义时使用 Matplotlib [英] Using Matplotlib when DISPLAY is undefined
问题描述
在远程机器上(例如在Travis CI上)使用Matplotlib时,我经常遇到与未设置DISPLAY环境变量有关的运行时错误.按照建议,我在测试脚本的开头通过 matplotlib.use
设置了 Agg
后端,并确保没有调用 show()代码>.尽管如此,我仍然收到如下错误:
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 Agg
backend via matplotlib.use
at the beginning of my test scripts, and have ensured there are no calls to show()
. Nevertheless, I still get errors such the following:
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?
推荐答案
Using xvfb
works, see Travis's
GUI & Headless browser testing documentation.
p.s. matplotlib.use('Agg')
技巧对我有用.
p.s. The matplotlib.use('Agg')
trick works for me.
这篇关于当 DISPLAY 未定义时使用 Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!