35

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?

Chris Fonnesbeck
  • 4,143
  • 4
  • 29
  • 30
  • Have you seen all the answers to this: http://stackoverflow.com/questions/2801882/generating-a-png-with-matplotlib-when-display-is-undefined 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 Jan 23 '14 at 23:29
  • 2
    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 Jan 23 '14 at 23:38
  • 4
    It is necessary that the `matplotlib.use('Agg')` line is before any other matplotlib imports. That snagged me for a while. See [this answer](http://stackoverflow.com/a/4706614/1220158). – Kelsey May 07 '14 at 11:29
  • @Kelsey: How can you use the `matplotlib.use('Agg')` before actually importing matplotlib? Python would obviously complain that matplotlib is missing if you do as you say. – Shailen Sep 22 '14 at 16:50
  • 2
    @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 Sep 24 '14 at 17:15
  • 1
    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: http://www.davidketcheson.info/2015/01/13/using_matplotlib_image_comparison.html – David Ketcheson Feb 06 '15 at 11:03
  • The answer is similar, but the issue different than one flagged. This is issue is related to errors during building with Travis, which I've found to differ from local behavior. This is not a duplicate. – pylang Mar 31 '16 at 02:00

1 Answers1

21

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

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

ariddell
  • 3,413
  • 34
  • 32
  • 6
    "%pylab inline" also works. I tried above solution of using 'Agg' but it didn't work for me. I have jupyter installed on google cloud and use notebook on my pad. In this setup I was not able to display plots. – rkmalaiya Feb 24 '16 at 13:06
  • 1
    Agreed with @rkmalaiya, %pylab inline worked for me on jupyter notebook – charles gomes Mar 21 '16 at 18:48
  • `%matplotlib inline` works on Jupyter notebook. It makes matplotlib plot on notebook itself, instead of a `DEVICE`. – CᴴᴀZ Mar 07 '17 at 14:08