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.
If you use Arch Linux (distributions like
Manjaro
orAntegros
) simply type:And all will work perfectly!
After upgrading lots of packages (
Spyder
3 to 4,Keras
andTensorflow
and lots of their dependencies), I had the same problem today! I cannot figure out what happened; but the (conda-based) virtual environment that kept usingSpyder
3 did not have the problem. Although installingtkinter
or changing the backend,via matplotlib.use('TkAgg)
as shown above, or this nice post on how to change the backend, might well resolve the problem, I don't see these as rigid solutions. For me, uninstallingmatplotlib
and reinstalling it was magic and the problem was solved.... then, install
From all the above, this could be a package management problem, and BTW, I use both
conda
andpip
, whenever feasible.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 callingplt.savefig
:I found the answer on a github repository.
Try
import tkinter
because pycharm already installed tkinter for you, I looked Install tkinter for PythonYou can maybe try:
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.
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:instead of installing it with
pip
or directly in the virtual environment in Pycharm.Linux Mint 19. Helped for me:
P.S. Recompile python interpreter after package install.