Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing matplotlib-based plots in Travis CI

I have a package that has a module that helps users generate specific plots using matplotlib. When I call these functions in my unit tests inside Travis, I get the following error:

RuntimeError: Invalid DISPLAY variable

How do I fix that?

After generating the plot, my functions usually call pyplot.show(), which opens a window that needs to be closed. When I call these functions in my unit tests inside Travis, they get hung up forever.

How do I test that these plots are being generated using Travis CI?

like image 329
Douglas De Rizzo Meneghetti Avatar asked Feb 15 '16 06:02

Douglas De Rizzo Meneghetti


1 Answers

You can solve this without explicitly setting display by using the "agg" backend in matplotlib. This is necessary anyway, in my experience, in order to ensure consistency of the generated images. Just make sure you

 matplotlib.use('agg')

before you import pyplot or pylab. I've explained more here.

like image 192
David Ketcheson Avatar answered Oct 17 '22 21:10

David Ketcheson