from matplotlib.backends import _tkagg ImportError: cannot import name _tkagg
Solution 1
I just ran into this (Ubuntu 15.10 but same idea) and fixed it by:
sudo apt-get install tk-dev
pip uninstall -y matplotlib
pip --no-cache-dir install -U matplotlib
I think the third step was the critical one; if the cache is permitted then pip
appeared to be just using the previously-built installation of matplotlib
.
You can also manually remove the previously-built matplotlib; on Ubuntu it lives in ~/.cache/pip
somewhere. (I couldn't find a way for pip to clean up its cache unfortunately.)
Solution 2
Python 3.7.4 on Windows 10, replacing NavigationToolbar2TkAgg with NavigationToolbar2Tk works for me.
Change from:
from matplotlib.backends.backend_tkagg import ( FigureCanvasTkAgg, NavigationToolbar2TkAgg)
To:
from matplotlib.backends.backend_tkagg import ( FigureCanvasTkAgg, NavigationToolbar2Tk)
Solution 3
One answer to this question suggests the error is due to a missing Visual C++ Redistributable for Visual Studio 2015 . Installing it solved the problem for me.
Solution 4
Try following this scenario:
sudo apt-get update
sudo apt-get install tk tk-dev
sudo pip uninstall matplotlib
sudo pip install matplotlib
EDIT:
Try:
sudo pip uninstall matplotlib
sudo apt-get install python-matplotlib
EDIT2:
sudo apt-get install tk8.6-dev
sudo apt-get remove python-matplotlib
sudo apt-get install python-matplotlib
QPTR
Updated on October 21, 2020Comments
-
QPTR over 3 years
While trying to run this example to test how matplotlib works with Tkinter, I am getting the error:
(env)fieldsofgold@fieldsofgold-VirtualBox:~/new$ python test.py Traceback (most recent call last): File "test.py", line 7, in <module> from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 13, in <module> import matplotlib.backends.tkagg as tkagg File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 7, in <module> from matplotlib.backends import _tkagg ImportError: cannot import name _tkagg
Using the solution provided here, I've tried to uninstall matplotlib and install the tk and tk-dev packages by using these commands :
sudo apt-get install tk8.5 sudo apt-get install tk-dev
and then re-installing matplotlib again by
pip install matplotlib
but I am still getting the same error. Any help would be appreciated. I am using Ubuntu 14.04 on VirtualBox and working inside a virtualenv environment. Thanks so much. -
YOBA over 8 years@QPTR Ok, then avoid using pip, use the same package manager, I just made an edit
-
QPTR over 8 yearsHmm, it uninstalls fine. However, it gives
python-matplotlib is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 515 not upgraded.
aftersudo apt-get install python-matplotlib
. Can import matplotlib fine though from the interpreter. But it still doesn't work. -
YOBA over 8 yearsWeird, are tk and tk-dev being installed correctly?
-
QPTR over 8 yearsAs far as I can tell, yes, because when I try to install them it says
tk is already the newest version.
tk-dev is already the newest version.
Is there any other way to know? -
YOBA over 8 yearsNo, I guess you're right, the only thing I can think of is matplotlib not finding tk when building, in my case it works fine with tk8.6-dev, give it a try: again sudo apt-get install tk8.6-dev, then sudo apt-get remove python-matplotlib , finally sudo apt-get install python-matplotlib
-
QPTR over 8 years
tk8.6dev
or justtk8.6
? 2 options available when I do anapt-cache search tk8.6
-
YOBA over 8 yearsboth if you find them in your repository, 8.5 is not working with matplolib for ubuntu 14.04.
-
QPTR over 8 yearsNot working. Shall I try installing matplotlib with pip?
-
YOBA over 8 yearsOk, give it a try, it's probably a good idea, make sure you uninstall tk tk-dev tk8.5, matplotlib and leave only tk8.6 and tk8.6dev then reinstall matplotlib with pip
-
QPTR over 8 yearssame error :( Uninstalled
tk
,tk-dev
,tk8.5
, then used pip to installmatplotlib
. Even triedsudo apt-get install python-matplotlib
but then the system couldn't find matplotlib. So uninstalled and installed with pip again. -
DanielSank over 8 yearsIt is not a good idea to
sudo pip install
packages like this. Use virtualenv. -
luka5z over 7 yearsI've found it the most convenient to use Ubuntu package and run
apt-get install python-matplotlib
orpython3-matplotlib
.