219
votes

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.

23
Forget 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 ?ImportanceOfBeingErnest
Are you actually using tkinter for anything, or did you just pick it as a plt backend?G. Anderson
@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
@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.johnwolf1987
@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.johnwolf1987

23 Answers

292
votes

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.

62
votes

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.

33
votes

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

sudo pacman -S tk

And all will work perfectly!

21
votes

Simple install

pip3 install PyQt5==5.9.2

It works for me.

18
votes

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.

10
votes

I too had this issue in PyCharm. This issue is because you don't have tkinter module in your machine.

To install follow the steps given below (select your appropriate os)

For ubuntu users

 sudo apt-get install python-tk

or

 sudo apt-get install python3-tk

For Centos users

 sudo yum install python-tkinter

or

 sudo yum install python3-tkinter

for Arch Users

  sudo pacman -S tk

or

  sudo pamac install tk

For Windows, use pip to install tk

After installing tkinter restart your Pycharm and run your code, it will work

6
votes

This worked with R reticulate. Found it here.

1: matplotlib.use( 'tkagg' ) or 2: matplotlib$use( 'tkagg' )

For example:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style

import matplotlib
matplotlib.use( 'tkagg' )


style.use("ggplot")
from sklearn import svm

x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]

plt.scatter(x,y)
plt.show()
4
votes

The answer has been given a few times but it is not obvious, one needs to install graphics, this works.

pip3 install PyQt5

I hope this saves somebody some time.

3
votes

I added %matplotlib inline and my plot showed up in Jupyter Notebook.

2
votes

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

And this worked for me

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')
2
votes

I installed python3-tk , on Ubuntu 20.04 and using WSL2

import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib
    matplotlib.use( 'tkagg')

and then I installed [GWSL][1] from the Windows Store which seems to solve problem of WSL2 rendering out of the box

Also see if you can render plots after recent WSL2 GUI updates? [1]: https://www.microsoft.com/en-in/p/gwsl/9nl6kd1h33v3?cid=storebadge&activetab=pivot:overviewtab#

1
votes

After upgrading lots of packages (Spyder 3 to 4, Keras and Tensorflow 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 using Spyder 3 did not have the problem. Although installing tkinter 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, uninstalling matplotlib and reinstalling it was magic and the problem was solved.

pip uninstall matplotlib

... then, install

pip install matplotlib

From all the above, this could be a package management problem, and BTW, I use both conda and pip, whenever feasible.

1
votes

The comment by @xicocaio should be highlighted.

tkinter is python version-specific in the sense that sudo apt-get install python3-tk will install tkinter exclusively for your default version of python. Suppose you have different python versions within various virtual environments, you will have to install tkinter for the desired python version used in that virtual environment. For example, sudo apt-get install python3.7-tk. Not doing this will still lead to No module named ' tkinter' errors, even after installing it for the global python version.

1
votes

I've solved putting matplotlib.use('TkAgg') after all import statments. I use python 3.8.5 VSCODE and anaconda. No other tricks worked.

1
votes

If using Jupyter notebook try the following:

%matplotlib inline

This should render the plot even if not specifying the

plt.show()

command.

0
votes

Linux Mint 19. Helped for me:

sudo apt install tk-dev

P.S. Recompile python interpreter after package install.

0
votes

Just in case if this helps anybody.

Python version: 3.7.7 platform: Ubuntu 18.04.4 LTS

This came with default python version 3.6.9, however I had installed my own 3.7.7 version python on it (installed building it from source)

tkinter was not working even when the help('module') shows tkinter in the list.

The following steps worked for me:

  1. sudo apt-get install tk-dev.

rebuild the python: 1. Navigate to your python folder and run the checks:

cd Python-3.7.7
sudo ./configure --enable-optimizations
  1. Build using make command: sudo make -j 8 --- here 8 are the number of processors, check yours using nproc command.
  2. Installing using:

    sudo make altinstall
    

Don't use sudo make install, it will overwrite default 3.6.9 version, which might be messy later.

  1. Check tkinter now
    python3.7 -m tkinter
    

A windows box will pop up, your tkinter is ready now.

0
votes

When I ran into this error on Spyder, I changed from running my code line by line to highlighting my block of plotting code and running that all at once. Voila, the image appeared.

0
votes

You can change the matplotlib using backend using the from agg to Tkinter TKAgg using command

matplotlib.use('TKAgg',warn=False, force=True)
0
votes

If you install python versions using pyenv on Debian-based systems, be sure to run sudo apt install tk-dev before pyenv install. If it's already installed, remove it with pyenv uninstall and install it again after install tk-dev. Therefore, there is no need to set any env variables when running pyenv install.

0
votes

Works if you use some third party code in your project. It probably contains the following line

matplotlib.use('Agg')

Search for it and comment it out.

If you have no clue about what it is you are probably not using this part of the code.

Solutions about using another backend GUI may be cleaner, so choose your fighter.

0
votes

The solution that worked for me:

  1. Install tkinter

  2. import tkinter into the module

  3. make sure that matplotlib uses (TkAgg) instead of (Agg)

    matplotlib.use('TkAgg')

0
votes

Beware of the import order in your code, I spent a whole day going through this answers and ended up solving the problem by importing bt before anything else and then using the .use('TkAgg') statement (for some reason importing bt changes the matplotlib backend to 'Agg')