
If you encoutered the following warning when using pyplot in pycharm:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()
Code language: CSS (css)
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
Code language: HTTP (http)
Usually this error appears when you pip install matplotlib
but do not have a python module for GUI display.
matplotlib does not depend on any GUI backend because some people need matplotlib without any GUI backend
You can try the solutions listed below
Solution 1: is to install the GUI backend tk
Try to install tkinter through the Linux bash terminal using the following command:
sudo apt-get install python3-tk
Code language: JavaScript (javascript)
instead of installing it with pip or directly in the virtual environment in Pycharm.
Solution 2: install any of the matplotlib supported GUI backends
You can also fix the issue by installing other matplolib GUI backends like Qt5Agg, GTKAgg, Qt4Agg, etc
for example pip install pyqt5
will fix the issue also
Solution 3:Use savefig instead
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [5, 7, 4])
plt.savefig("mygraph.png")
Code language: CSS (css)
Solution 4:
If you use Arch Linux (distributions like Manjaro or Antegros) simply type:
sudo pacman -S tk
Solution 5:
pycharm already installed tkinter so you may try import tkinter
import tkinter
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
plt.plot([1,2,3],[5,7,4])
plt.show()
Code language: JavaScript (javascript)