Fixing python matplotlib ImportError: Cannot load backend ‘TkAgg’ which requires the ‘tk’ interactive framework, as ‘headless’ is currently running

Python matplotlib ImportError: Cannot load backend ‘TkAgg’ which requires the ‘tk’ interactive framework, as ‘headless’ is currently running is a very popular error which occurs when you use matplotlib in google colab or in a docker container or in some other environment.

In this blog post I attempt to present a clear explanation of why this error takes place and how you to solve it, I am also going to explain other ways to solve the error if possible.

Exploring the python matplotlib ImportError: Cannot load backend ‘TkAgg’ which requires the ‘tk’ interactive framework, as ‘headless’ is currently running

This is an error which occurs when you use matplotlib in google colab or in a docker container or in some other environment.

Do not mix between errors. Make sure the error message looks like the error message bellow

                                                                       #
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework
                                                                       #

In the sections bellow we will explain the source of the problem and propose many possible fixes.

Solution 1 : Set the environment variable using ( export MPLBACKEND=TKAgg ) or upgrade Matplotlib

First, I should explain that this is not an error specific to google colab or to docker, the error might occur in other environments too.

The error is just more popular with Google colab and docker.

The first option you can explore is to upgrade Matplotlib. Using the command bellow for example.

                                                                       #
pip install matplotlib==3.6.0
                                                                       #

You can choose another stable version like Matplotlib version 3.0

The other option is to set the environment variable like this

                                                                       #
export MPLBACKEND=TKAgg
                                                                       #

If this method does not work pleas try the next method.

Solution 2 : Correctly import TKAgg using matplotlib.use(‘TKAgg’)

Sometimes, the error occurs when you forget to import TKAgg. You should use the following lines of code before writing your code

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

Also make sure to restart the kernel after making the change. Try the next method if this one was not enough.

Solution 3 : Use matplotlib.use(‘Agg’) instead of using matplotlib.use(‘TkAgg’)

A lot of people do not know that TkAgg is not the only solution, it is actually just one of many other solutions you can use, one of those solutions is Agg. Agg is a Matplotlib “backend” just like TkAgg .

You can use Agg like this

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

I hope this method has solved your issue, thank you for reading this post to the end, good luck with your project.

Summing-up : 

That is it guys, this is the end of this article aka guide, I hope you found it useful in solving your problem, make sure to support our work on Kofi, you do not have to but hey you can donate to the team.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/