_tkinter.TclError: no display name and no $DISPLAY environment variable

_tkinter.TclError: no display name and no $DISPLAY environment variable is an error which occurs because matplotlib uses the Xwindows backend by default.

In this article I am explaining why the error is happening and check out solutions that may work for your particular case.

Explaining the Error : _tkinter.TclError: no display name and no $DISPLAY environment variable

First, let us try and reproduce the error, in the example we chose is an example where the developer is using pyplot in matplotlib in Python. Bellow is the code he uses

                                                                       #
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(60)
y = np.random.randn(60)
plt.scatter(x, y, s=20)
out_png = 'path/to/store/out_file.png'
plt.savefig(out_png, dpi=150)
                                                                       #

The error message should look like the error in the example bellow.

                                                                       #
_tkinter.TclError: no display name and no $DISPLAY environment variable
                                                                       #

Bellow is a number of tested solutions that I have tried and worked for me.

Solution 1 : choose a non-interactive backend

First, we should understand that the the error happens in Python since Xwindows is chosen by default as backend. We do not want to use the Xwindows backend.

Instead choose a non-interactive backend, to do that, go to the path bellow

                                                                       #
.config/matplotlib/matplotlibrc
                                                                       #

And add the following line

                                                                       #
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc
                                                                       #

I hope this works for you, if not try the solution bellow.

Solution 2 : avoid using the Xwindows backend , work with use(‘Agg’) instead

Another solution to avoid using the Xwindows backend and use a non-interactive backend is to add this code to your file/code

                                                                       #
import matplotlib
matplotlib.use('Agg')
                                                                       #

Please do this before importing pyplot.

Solution 3 : use remoteMachine

The last way to achieve the same result of the solutions above. Is to run the command bellow when connecting to the server.

                                                                       #
ssh -X remoteMachine
                                                                       #

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

The solutions above should be enough to solve the error _tkinter.TclError: no display name and no $DISPLAY environment variable

I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/