"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
- for example
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 needmatplotlib
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.
Solution 5
Try import tkinter
because pycharm already installed tkinter for you, I looked Install tkinter for Python
You can maybe try:
import tkinter
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
plt.plot([1,2,3],[5,7,4])
plt.show()
as a tkinter-installing way
I've tried your way, it seems no error to run at my computer, it successfully shows the figure. maybe because pycharm have tkinter as a system package, so u don't need to install it. But if u can't find tkinter inside, you can go to Tkdocs to see the way of installing tkinter, as it mentions, tkinter is a core package for python.
Related videos on Youtube
johnwolf1987
Updated on March 17, 2022Comments
-
johnwolf1987 about 2 years
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.
-
ImportanceOfBeingErnest almost 5 yearsForget about the graph for a moment. Your problem is to install tkinter. Did you try any of the available solutions to that, e.g. stackoverflow.com/questions/4783810/install-tkinter-for-python ?
-
G. Anderson almost 5 yearsAre you actually using tkinter for anything, or did you just pick it as a plt backend?
-
johnwolf1987 almost 5 years@ImportanceOfBeingErnest: Thank you for the hint. I will indeed focus on installing tkinter first. I will have a look at the link you provided and see if I can make anything out of it.
-
johnwolf1987 almost 5 years@G.Anderson: I had no idea what tkinter was before I ran into this error with matplotlib. Now I am trying to install it just to be able to show graphs (so yes, I guess I just picked it as a plt backend). If you know of any other way (i.e. without using tkinter), I would be glad to hear it.
-
G. Anderson almost 5 yearsThere are a number of backends you can use. Here is an answer about cycling though backends until you find one that works with your installation
-
johnwolf1987 almost 5 years@ImportanceOfBeingErnest: Thanks a lot for your help. I installed tkinter in the bash terminal using
apt-get install python3-tk
(from the link you sent me) and it seemed to have worked, as I can now display graphs. Also I don't need to import tkinter in my script. -
johnwolf1987 almost 5 years@G.Anderson: I ran the script to cycle through the different backends. It turns out that only TkAgg Is Available ! Indeed, I had just installed it prior to testing the script.
-
ImportanceOfBeingErnest almost 5 yearsModern matplotlib versions will automatically cycle through available toolkits; so if tkinter isn't installed (and none of the other options are) it will fall back to
agg
backend. -
8.8.8.8 over 4 yearsOn arch linux,
pacman -S --needed python-pyqt5
fixed the problem. -
not2qubit over 3 yearsI face a similar problem and have managed to use anything but
Tcl/Tk
with python as reported here. IMO there must be a way to load Tcl/Tk as an external DLL and use thetkinter
as an alias.
-
-
johnwolf1987 almost 5 yearsThank you for your suggestion. Unfortunately, it does not work (
ModuleNotFoundError: No module named 'tkinter'
). I will try to install tkinter some other way. -
johnwolf1987 almost 5 yearsActually, I also realised that since I installed tkinter, I don't need to add the first two lines of my code anymore (
import matplotlib
andmatplotlib.use('TkAgg')
) -
Iulian Pinzaru almost 4 yearsThis was very helpful due to the fact that I didn't have to reload my interactive ipython console lol. Thanks
-
Eldrad almost 4 yearsFor completeness, under OpenSuse Leap the package I had to install is called
python3-matplotlib-tk
. -
xicocaio over 3 yearsThis answer could probably mention that
tkinter
is python version-specific in the sense that this particular command will installtkinter
exclusively for your default version of python. Suppose you have different python versions installations for various virtual envs. In that case, you will have to install it for the desired python version used in that working venv. For example, in my case:sudo apt-get install python3.7-tk
. Not knowing this made me struggle a reasonable amount of time getting no module name ' tkinter' errors, even after installing it for my global python version. -
WhaSukGO over 3 yearsYup, it works! I installed it inside my virtual env via PyCharm's terminal.
-
oraclesoon over 3 yearsI execute "pip3 install PyQt5" without specifying version number.
-
arame3333 over 3 yearsI had to install a Linux Bash Terminal on my PC in order to run this command. And it worked! See howtogeek.com/249966/…
-
Vicrobot over 3 yearsOr use
sudo apt-get install python3.8-tk
if your python version is 3.8 -
Larry Cai about 3 years
matplotlib.use('TkAgg')
gave[Previous line repeated 987 more times] RecursionError: maximum recursion depth exceeded
for me. I 'fixed' it by starting a new Python Console (in PyCharm). -
arame3333 about 3 yearsIt doesn't really solve the underlying problem, but in my case this is the preferred solution in that it is better to save the image rather than show it, and this will often be the case.
-
Senthuran about 3 yearsI also encountered the same problem...I'm using linux so I have used this command to install the tkinter
sudo apt-get install python3-tk
-
Albert G Lieu about 3 yearsThis is the only answer solved my problem
-
alexkaz about 3 yearsWorked for me on Ubuntu 16.04, PyCharm Prof
-
Néstor Waldyd about 3 yearsFor Fedora: sudo dnf install python3-tkinter
-
Shubham Pawar almost 3 yearsDont know how this is not approved. Only this method worked
-
Crawl.W over 2 yearspip how install tkinter? there are no corresponding item in search results.
-
Gearoid Murphy over 2 yearsThis worked when running a python binary via Bazel. +1
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
Gino Mempin over 2 yearsThis does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
-
Admin over 2 yearsSir, Q: he was not able to see the chart correctly. A: By installing the software you are able to fix that problem. I had the same problem and this was how I fixed it in my personal experience. Thanks.
-
NobodySomewhere over 2 yearsFor me, "pip install tk" did not work (Im using windows 10). What worked is reinstalling pythin through downloaded executable, with the option "Modify", and then checking "tcl/tk and IDLE" checkbox.
-
Gino Mempin over 2 yearsThen please edit to add more details and steps to your answer, which is basically "install anaconda". Installing Anaconda is just the 1st step. How does it solve the problem with matplotlib and tkinter? How do they install the other packages? What specific commands do they need to run? It's missing some more details to make it a full answer.
-
Gino Mempin over 2 yearsThis is already mentioned in this answer and this answer. There is no need to be repeating existing answers. Once you have enough reputation, you can vote them up instead.
-
Gregor over 2 yearsI think this is actually the right solution, because it is the easiest. Only need to install it where needed, e.g. by running : pip3 install PyQt5
-
logicOnAbstractions over 2 yearsI'm afraid to say this is like the 3rd time I end up here. Perhaps by commenting on it, I stand a better chance of remembering the "trick" next time I try this again...
-
Aesir over 2 yearsThis was exactly the problem I had. Re-downloading the python installer for windows and adding this to the python installation via modify (after it had upgraded). No re-creation of virtualenvs was required.
-
Simas Joneliunas about 2 yearsWhile this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review