Solving Matplotlib UserWarning – when using agg the non-GUI backend

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure is an error which occurs when there is a compatibility issue with the TkAgg matplolib GUI backend or a TkAgg import issue or many other reasons.

Today I try to explain why this error takes place and how to solve it, I will also add other solutions that could solve the error if possible.

Exploring Matplotlib UserWarning – when using agg the non-GUI backend

This is an error which occurs when there is a compatibility issue with the TkAgg matplolib GUI backend or a TkAgg import issue or many other reasons.

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

Solving Matplotlib UserWarning when using agg the non-GUI backend

In the sections bellow we will explain the root of the error more and propose some possible fixes.

Solution 1 : use other matplolib GUI backends

The first solution is to use other matplolib GUI backends, I recommend using the GTKAgg GUI backend or the Qt5Agg GUI backend.

You do not need a specific backend to be able to plot your data, you do not even need to use the TkAgg backend, you can just use another one.

This is how you can change the backend in your code.

Solving Matplotlib UserWarning when using agg the non-GUI backend - 1

This solution should be enough to solve the issue, if that is not the case you can use the solution bellow.

Solution 2 : correctly install tkinter and do not even import ‘TkAgg’ and matplotlib in your code

The good thing about using tkinter is that you do not have to import TkAgg or any other GUI backend you do not even need to import matplotlib.

Installing tkinter using pip should be easy but sometimes this fails and you might get an error.

Fixing Python PermissionError Errno 13 Permission denied-2

The command above might result in the error message bellow.

Fixing Python PermissionError Errno 13 Permission denied-3

You should install tkinter on your system by using the standard command line (terminal). You can use the bash command bellow on linux.

Solving Matplotlib UserWarning when using agg the non-GUI backend-4

The solution should be enough to solve your issue. If not, try the other solutions bellow

Solution 3 : install PyQt5 or use sudo pacman

Another easy fix is to install PyQt5using pip or pip3

Solving Matplotlib UserWarning when using agg the non-GUI backend-5

Specify the exact version of PyQt5 using v.v.v.

Use pacman, you should do that on the terminal. As far as I know This works for only Arch Linux distributions. But you can still try with other distributions.

Solving Matplotlib UserWarning when using agg the non-GUI backend-7

Bellow we have the last fix, with two options that can be used.

Solution 4 : use plt.savefig instead of plt.show()

In headless mode, plt.show() is not going to plot anything. Instead you should use plt.savefig.

You should use the line of code bellow.

Solving Matplotlib UserWarning when using agg the non-GUI backend-8

Instead of this line of code

Solving Matplotlib UserWarning when using agg the non-GUI backend-9

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

I hope this article has helped you achieve your objective with solving the error UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. If you like the effort we did here, please consider donating to our Kofi account located at the top of this page. Keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/