34
votes

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?

1
Have you seen all the answers to this: stackoverflow.com/questions/2801882/… One of them mentions modifying matplotlib source a tiny amount, this might help. Generally there are several answers there with different solutions, and they all seem valid. Maybe one will work for you too. - Aleksander Lidtke
I've tried all the suggestions from that post that I am able to implement (setting DISPLAY to localhost:0, setting the backend to Agg), but none work. Its not feasible for me to modify the source and I am not able to add ssh flags, as far as I know. - Chris Fonnesbeck
It is necessary that the matplotlib.use('Agg') line is before any other matplotlib imports. That snagged me for a while. See this answer. - Kelsey
@shailenTJ: Yes, you need to import matplotlib, but nothing else (e.g. not matplotlib.pyplot), and don't do anything before setting the backend. - Kelsey
I think this question should not be a duplicate. The OP is probably using a test harness, such as nose, which imports all files before running the tests. The order of imports cannot be directly controlled. The solution is to call nose from a script in which one first sets the backend; see my explanation near the end of this post: davidketcheson.info/2015/01/13/… - David Ketcheson

1 Answers

21
votes

Using xvfb works, see Travis's GUI & Headless browser testing documentation.

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