18

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.

Community
  • 1
  • 1
QPTR
  • 1,620
  • 7
  • 26
  • 47

5 Answers5

28

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.)

lmjohns3
  • 7,422
  • 5
  • 36
  • 56
  • I've found it the most convenient to use Ubuntu package and run `apt-get install python-matplotlib` or `python3-matplotlib`. – luka5z Aug 11 '16 at 20:58
18

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)

Harry
  • 1,147
  • 13
  • 13
1

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
YOBA
  • 2,759
  • 1
  • 14
  • 29
  • @QPTR Ok, then avoid using pip, use the same package manager, I just made an edit – YOBA Aug 24 '15 at 17:51
  • Hmm, 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.` after `sudo apt-get install python-matplotlib` . Can import matplotlib fine though from the interpreter. But it still doesn't work. – QPTR Aug 24 '15 at 17:57
  • Weird, are tk and tk-dev being installed correctly? – YOBA Aug 24 '15 at 18:02
  • As 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? – QPTR Aug 24 '15 at 18:04
  • No, 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 – YOBA Aug 24 '15 at 18:09
  • `tk8.6dev` or just `tk8.6` ? 2 options available when I do an `apt-cache search tk8.6` – QPTR Aug 24 '15 at 18:11
  • both if you find them in your repository, 8.5 is not working with matplolib for ubuntu 14.04. – YOBA Aug 24 '15 at 18:12
  • 1
    Not working. Shall I try installing matplotlib with pip? – QPTR Aug 24 '15 at 18:15
  • Ok, 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 – YOBA Aug 24 '15 at 18:18
  • same error :( Uninstalled `tk`, `tk-dev`, `tk8.5`, then used pip to install `matplotlib`. Even tried `sudo apt-get install python-matplotlib` but then the system couldn't find matplotlib. So uninstalled and installed with pip again. – QPTR Aug 24 '15 at 18:31
  • It is not a good idea to `sudo pip install` packages like this. Use virtualenv. – DanielSank Oct 13 '15 at 22:15
1

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.

DrawT
  • 232
  • 4
  • 6
1

replacing NavigationToolbar2TkAgg with NavigationToolbar2Tk also works in Python 3.8.5 (MacOS Catalina).