A few weeks after releasing my tutorial for installing Jupyter Notebook on Android, I ran into this issue of Matplotlib: TclError: no display name and $DISPLAY environment variable, whenever I tried to use it in Jupyter Notebook on Android. This problem got me puzzled for a while because I knew it used to work when I was writing the tutorial. Luckily, there is a one-line fix for this issue. So, in case you don’t have time for any other details, just put this line at the beginning of the notebook:

%matplotlib inline
Matplotlib no display ERROR on Android

Check the example, it works!

Matplotlib Backends

You may be wondering what this line does and maybe you even see it in some notebooks. The %matplotlib is a magic command which performs setup of Matplotlib. All magic commands start with “%” and they usually provide functions not supported by the language itself. There is a large number of them already build-in IPython kernel and some of them are really interesting (documentation).

Back to the %matplotlib, you can pass argument — name of backend ― to this command. Because the library can have many different use cases, it provides different backends for embedding plots into applications or saving them as an image (matplotlib backends). IPython provides this inline backend, which renders plots directly in the notebook as static images and there is no need for DISPLAY variable. Because it seems that the GNURoot Debian app fails simulate this variable and every other backend requires it.

In case that you are using matplotlib on your PC, you should definitely check out the %matplotlib notebook option.This special backend intended for notebooks provides a simple interface (moving, zooming or saving) for interacting with plot and it even allows creating interactive 3D plots or live updating graphs.

Even though you don’t have to care about backends most of the time, sometimes it may be really useful.