All Collections
Tips and Tricks
Language-specific issues
Using matplotlib with no display attached
Using matplotlib with no display attached

Solving "RuntimeError: Invalid DISPLAY variable" in Python.

Shahar Zaks avatar
Written by Shahar Zaks
Updated over a week ago

Trying to run matplotlib  code on Code Ocean, you may encounter the following error:

RuntimeError: Invalid DISPLAY variable.

matplotlib  is trying to show the figure but cannot find a display. The recommended solution is to set the default backend¹ by creating a matplotlib  configuration file - a file called matplotlibrc consisting of the following line:
backend: Agg.

Alternatively, you can set the backend manually at the top of your python script:

import matplotlib
matplotlib.use('Agg') # set the backend before importing pyplot

import matplotlib.pyplot as plt # etc. etc.

"What is a backend?", matplotlib.org.

Did this answer your question?