Last updated: Apr 13, 2024
Reading time·3 min
The Matplotlib error "_tkinter.TclError: no display name and no $DISPLAY environment variable" occurs because of the default Matplotlib backend
To solve the error set, the backend to Agg
(Anti-Grain Geometry engine) or
set the DISPLAY
environment variable.
The first thing you should try is setting your
Matplotlib backend
to Agg
.
import matplotlib matplotlib.use('Agg') import numpy as np import matplotlib.pyplot as plt x = np.random.randn(90) y = np.random.randn(90) print(x) plt.scatter(x, y, s=80, edgecolors='blue', facecolors='none') plt.savefig('my-img.png', dpi=150)
Make sure to add the following 2 lines above the line that imports
matplotlib.pyplot
(import matplotlib.pyplot as plt
).
If you set the backend to Agg
after importing pyplot
, the error will
persist.
The AGG
static backend is used for png
images. It produces high-quality
images using the Anti-Grain Geometry engine.
The renderer is capable of writing to a file as shown in the code sample.
You can also set the backend via the MPLBACKEND
environment variable.
Issue the following command in your terminal.
export MPLBACKEND=Agg
If you ever need to unset the environment variable, issue the following command.
unset MPLBACKEND
The command unsets the MPLBACKEND
environment variable.
Agg
in your matplotlibrc
config fileYou can also set the Matplotlib backend to Agg
in your matplotlibrc
file.
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc
Alternatively, you can open the ~/.config/matplotlib/matplotlibrc
with your
preferred text editor and add the line backend: Agg
.
# Using `gedit` sudo gedit ~/.config/matplotlib/matplotlibrc # Using `nano` sudo nano ~/.config/matplotlib/matplotlibrc
Once you open your matplotlibrc
config file, add the following line.
backend: Agg
DISPLAY
environment variable to :0.0
Another thing you can try is to set the DISPLAY
environment variable to
:0.0
.
First, get your default value of the DISPLAY
variable.
echo $DISPLAY
Now, set the DISPLAY
variable to :0.0
.
export DISPLAY=:0.0
Every X server has a display name of the form
hostname:displaynumber.screennumber
.
This information is used by applications to determine how they should connect to the server and which screen they should use by default.
The :0.0
value relates to the first screen that is attached to your first
display on localhost.
This often helps when you try to run a Tkinter application on a Raspberry Pi.
If that didn't work revert your DISPLAY
variable to the previous value that
you got from running echo $DISPLAY
.
Note that rendering images and graphs on a server is not possible because servers don't have a display.
-X
optionIf you get the error when connecting to a remote machine, try to use the -X
option.
ssh -X your_user@your_hostname # or ssh -X your_hostname
The X Windows system is a complete, cross-platform client-server system for managing GUIs (graphic user interfaces) on single computers and on networks of computers.
This often helps if you're trying to render images, plots, etc.
You will also get the error if you try to use Tkinter on Google Colab.
You aren't able to use Tkinter on Google Colab because Colab doesn't support interactive screens out of the box.
Servers don't have a display, so trying to render interactive GUIs, images and graphs is not an option.
You can learn more about the related topics by checking out the following tutorials: