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

Solution 1:

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.

Solution 2:

In my case, the error message was implying that I was working in a headless console. So plt.show() could not work. What worked was calling plt.savefig:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [5, 7, 4])
plt.savefig("mygraph.png")

I found the answer on a github repository.

Solution 3:

If you use Arch Linux (distributions like Manjaro or Antegros) simply type:

sudo pacman -S tk

And all will work perfectly!

Solution 4:

Simple install

pip3 install PyQt5==5.9.2

It works for me.