Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm

I am trying to plot a simple graph using pyplot, e.g.:

import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()

but the figure does not appear and I get the following message:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

I saw in several places that one had to change the configuration of matplotlib using the following:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

I did this, but then got an error message because it cannot find a module:

ModuleNotFoundError: No module named 'tkinter'

Then, I tried to install "tkinter" using pip install tkinter (inside the virtual environment), but it does not find it:

Collecting tkinter
  Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter

I should also mention that I am running all this on Pycharm Community Edition IDE using a virtual environment, and that my operating system is Linux/Ubuntu 18.04.

I would like to know how I can solve this problem in order to be able to display the graph.

like image 459
johnwolf1987 Avatar asked Oct 05 '22 13:10

johnwolf1987


People also ask

How do you solve UserWarning matplotlib is currently using AGG which is a non-GUI backend so Cannot show the figure?

To Solve UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure Error Here Error is mentioning that You must have installed GUI backend So that Just Install the GUI backend tk Using this command: sudo apt-get install python3-tk Second solution is You just need to install ...

What does matplotlib use (' AGG ') do?

The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.

Is matplotlib thread safe?

Matplotlib is not thread-safe: in fact, there are known race conditions that affect certain artists. Hence, if you work with threads, it is your responsibility to set up the proper locks to serialize access to Matplotlib artists. You may be able to work on separate figures from separate threads.


1 Answers

Solution 1: is to install the GUI backend tk

I found a solution to my problem (thanks to the help of ImportanceOfBeingErnest).

All I had to do was to install tkinter through the Linux bash terminal using the following command:

sudo apt-get install python3-tk

instead of installing it with pip or directly in the virtual environment in Pycharm.

Solution 2: install any of the matplotlib supported GUI backends

  • solution 1 works fine because you get a GUI backend... in this case the TkAgg
  • however you can also fix the issue by installing any of the matplolib GUI backends like Qt5Agg, GTKAgg, Qt4Agg, etc
    • for example pip install pyqt5 will fix the issue also

NOTE:

  • usually this error appears when you pip install matplotlib and you are trying to display a plot in a GUI window and you do not have a python module for GUI display.
  • The authors of matplotlib made the pypi software deps not depend on any GUI backend because some people need matplotlib without any GUI backend.
like image 471
johnwolf1987 Avatar answered Oct 23 '22 12:10

johnwolf1987