matplotlib example + Anaconda3 crashes with _tkinter.TclError

12 views
Skip to first unread message

fun...@gmail.com

unread,
Sep 30, 2014, 8:03:07 PM9/30/14
to anac...@continuum.io
this example:

http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html

which appears to be coded to run in both python 2.7 and 3.x

crashes like this when I run it:

------------ begin log

$ ./embedding_in_tk.py
Traceback (most recent call last):
  File "C:\Users\jcano\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\tkagg.py", line 13, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
_tkinter.TclError: invalid command name "PyAggImagePhoto"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./foo.py", line 36, in <module>
    canvas.show()
  File "C:\Users\jcano\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 349, in draw
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
  File "C:\Users\jcano\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\tkagg.py", line 20, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
_tkinter.TclError


------------ end log

My environment:

+ Windows 7
+ Anaconda 3 (Python 3.4.1) which I just downloaded yesterday
+ cygwin

I'm invoking my script from cygwin.  This seems to work fine for scripts that don't crash.  The beginning of my script looks like:

     1  #!/cygdrive/c/Users/jcano/AppData/Local/Continuum/Anaconda3/python
     2
     3  
     4
     5  import matplotlib
     6  matplotlib.use('TkAgg')
     7
     8  from numpy import arange, sin, pi
     9  from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    10  # implement the default mpl key bindings

I've used python a bit in the past but am completely new to TkInter and matplotlib.

I haven't tried Anaconda 2 (Python 2.7.8 based) yet.  (Presumably) the future of python is python 3 so it would be nice to have this working in python 3.

I've successfully run several other matplotlib examples with the environment above.

Matplotlib looks really cool!  I'm sure I'll be able to make it do what I want one way or another.

Regards,
   --fj

Carlos Córdoba

unread,
Sep 30, 2014, 9:44:34 PM9/30/14
to anac...@continuum.io
I reported this error before. It only occurs with the Tk backend and seems to be generated Matplotlib is compiled without Tk being installed.

Cheers,
Carlos

El 30/09/14 a las #4, fun...@gmail.com escribió:
--
Anaconda Community Support Group Brought to you by Continuum Analytics
---
You received this message because you are subscribed to the Google Groups "Anaconda - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to anaconda+u...@continuum.io.
To post to this group, send email to anac...@continuum.io.
Visit this group at http://groups.google.com/a/continuum.io/group/anaconda/.

jonathan cano

unread,
Oct 1, 2014, 5:46:55 PM10/1/14
to anac...@continuum.io
Ohhhh, I thought (assumed is more likely) that the default backend for me was 'TkAgg' but when I checked in IPython I see 'Qt4Agg' is my default backend.

As you point out, the problem is much simpler.  The following 3 programs illustrate:
  (1) Tkinter is not DOA
  (2) default backend is Qt4Agg and the simplest plot() example works with this
  (3) TkAgg backend fails to handle the simplest plot() example in (2)

#!/cygdrive/c/Anaconda3/python
import tkinter as Tk
root = Tk.Tk()
label = Tk.Label(text="hello world")
label.pack()
Tk.mainloop()
# we see here that tkinter is not 'dead on arrival' -- it works with
this most simple example
################ end file

#!/cygdrive/c/Anaconda3/python
import matplotlib as mpl
# print() shows the 'Qt4Agg' backend here
print("current backend is %s" % mpl.get_backend())
import matplotlib.pyplot as plt
plt.plot([1, 3, 2, 4])
plt.show()   # this works as expected
################ end file

#!/cygdrive/c/Anaconda3/python
import matplotlib as mpl
print("current backend is %s" % mpl.get_backend())
mpl.use('TkAgg')
print("changed backend.  it is now is %s" % mpl.get_backend())
import matplotlib.pyplot as plt
plt.plot([1, 3, 2, 4])
# this show() throws the exception mentioned in my original post
plt.show()
################ end file

Whoops, either Continuum doesn't have a 'TkAgg' regression or someone forgot to run regressions before releasising the current Anaconda.  Clearly they are not using TkAgg themselves.

Thanks for the info Carlos!


jonathan cano

unread,
Oct 1, 2014, 6:11:04 PM10/1/14
to anac...@continuum.io
pedantic correction:

I'm using python 3, downloaded 2014/09/29 but the version info from starting python is:

    > Python 3.4.1 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 17:27:11) [MSC v1.600 64 bit (AMD64)] on win32

so that is Anaconda 2.0.1 not "Anaconda 3"

Aaron Meurer

unread,
Oct 1, 2014, 6:18:55 PM10/1/14
to anaconda
When Anaconda reaches version 3.0 maybe we should pull a Microsoft and
skip to 4.0 to avoid confusion with Anaconda3 (the Python 3 version of
Anaconda).

Aaron Meurer

jonathan cano

unread,
Oct 2, 2014, 3:42:09 PM10/2/14
to anac...@continuum.io


On Wednesday, October 1, 2014 2:46:55 PM UTC-7, jonathan cano wrote:

As you point out, the problem is much simpler.  The following 3 programs illustrate:
  (1) Tkinter is not DOA
  (2) default backend is Qt4Agg and the simplest plot() example works with this
  (3) TkAgg backend fails to handle the simplest plot() example in (2)

...

This is still broken with the latest Anaconda release.   It appears that I am pissing into the wind trying to use the TkAgg backend with the Anaconda distribution.

Qt4Agg is the default backend (in Windows anyways).  For those of you doing GUI programming with the Anaconda distribution (i.e. mixing matplotlib and GUI calls) are you all using QT?

jonathan cano

unread,
Oct 2, 2014, 4:18:24 PM10/2/14
to anac...@continuum.io
this matplotlib + QT example works fine:


NOTE: it gives a deprecated warning about importing qt4_compat but this is easily fixed by changing qt4_compat to qt_compat.

it looks like Anaconda matplotlib is using PyQt4 rather than PySide (at least by default).  Is it correct to assume that using PyQt4 is the path of least resistance?  

Tkinter, PyQt, PySide, I don't really care, I'll learn what ever gives me the least support hassle.  Table stakes for "least support hassle" is not being broken out of the box as the TkAgg backend is (hence eliminating use of TkInter).

If you mix GUI programming with matplotlib please chime in with a brief summary of your views on which is the least hassle (most popular?)

Chris Barker

unread,
Oct 2, 2014, 4:37:03 PM10/2/14
to anaconda
On Thu, Oct 2, 2014 at 1:18 PM, jonathan cano <fun...@gmail.com> wrote:
 
Tkinter, PyQt, PySide, I don't really care, I'll learn what ever gives me the least support hassle.  Table stakes for "least support hassle" is not being broken out of the box as the TkAgg backend is (hence eliminating use of TkInter).

If you mix GUI programming with matplotlib please chime in with a brief summary of your views on which is the least hassle (most popular?)

I use wxPython with MPL. (via the wxMPL package, but that's a minor thing)

I recommend using the *agg back-ends as they get the most love, and you'll get identical results on all platforms (and other *agg back-ends) if you do that.

So I think it comes down to choosing the GUI toolki tyou like the best, or suports what you need, or folks you work with use...

IMHO the primary reason to choose TK is that it comes out of the box with Python -- but if it doesn't work with your Python distro -- there goes that.

-Chris



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

rainer...@esak.de

unread,
Apr 17, 2015, 6:39:07 AM4/17/15
to anac...@continuum.io
I found an easy solution for this problem :

uninstall pillow and matplotlib with conda

and install flawless versions from e.g. http://www.lfd.uci.edu/~gohlke/pythonlibs with pip

The versions of pillow and matplotlib in Anaconda have broken support for tkinter

/Reiner

patricio...@gmail.com

unread,
Jun 11, 2015, 3:19:10 AM6/11/15
to anac...@continuum.io, rainer...@esak.de
Awesome, this works. Thanks!

valin...@gmail.com

unread,
Jul 1, 2016, 12:39:11 PM7/1/16
to Anaconda - Public, rainer...@esak.de, patricio...@gmail.com
I had to uninstall the Anaconda version of matplotlib as well for this to work.  Kept getting the same error. 

Using:

[In]:
conda  
--version
[Out]:
conda
4.1.5

Python 3.5.1
OSX El Capitan 10.11.5

On Thursday, June 11, 2015 at 3:19:10 AM UTC-4, patricio...@gmail.com wrote:
Awesome, this works. Thanks!

linwood....@gmail.com

unread,
Jul 1, 2016, 1:05:15 PM7/1/16
to Anaconda - Public, rainer...@esak.de, patricio...@gmail.com, valin...@gmail.com

This (from the post above) solved my problem:

import matplotlib as mpl
print("current backend is %s" % mpl.get_backend())
mpl.use('TkAgg')


david.daiku...@gmail.com

unread,
Jul 29, 2016, 10:32:33 PM7/29/16
to Anaconda - Public, rainer...@esak.de
I spent the better part of 5 days trying to figure out why the Anaconda Python 3.5 kernel suddenly started crashing every time I ran my program. Finally I found this message and have resolved the issue. 

Yes indeed, the matplotlib module in Anaconda 4.1.1 is broken with respect to tkinter. By uninstalling matplotlib it and reinstalling from the wheel matplotlib-1.5.2-cp35-cp35m-win_amd64.whl from the UCI site, the problems went away.

Thank you, rainer...@esak.de !!

David

darius.b...@gmail.com

unread,
Aug 11, 2016, 2:16:57 PM8/11/16
to Anaconda - Public, rainer...@esak.de, david.daiku...@gmail.com
How do I use pip to install these WHL files? 

antho...@gmail.com

unread,
Aug 30, 2016, 3:33:48 AM8/30/16
to Anaconda - Public, rainer...@esak.de, david.daiku...@gmail.com, darius.b...@gmail.com
Managed to fix this as well.

$ conda uninstall matplotlib

This will remove the matplotlib version 1.5.1-np111py35_0. Follow by

$ pip install matplotlib

which install matplotlib version 1.5.2-cp35-cp35m-win_amd64.whl

Solved my kernel crashing problem. Thanks to suggestions the above.

mcg...@gmail.com

unread,
Oct 23, 2016, 11:04:29 PM10/23/16
to Anaconda - Public, rainer...@esak.de, david.daiku...@gmail.com, darius.b...@gmail.com, antho...@gmail.com
Hello,

Sorry in advance for my ignorance; I would appreciate some direction on this topic.

I am using Ubuntu 16.04.

1.  Any suggestions on a Linux version of matplotlib to replace with the conda version?  I am new to anaconda, and not an expert coder.

I presume that I must have a trial and error at what I find that others may have made available (below) or other?
anaconda search -t conda matplotlib



2.  If I must find a package NOT on the anaconda search... then suggestions?  And, then to use 'pip' to reinstall matplotlib, won't pip install on/in the Ubuntu python?  How do you run pip so that conda/anaconda will use/recognize it, AND so I don't mess up Ubuntu's python?

I found two possible ways:

1. A non-preferred way I believe:
pip install --target=d:\somewhere\other\than\the\default package_name

which I presume if anaconda is installed at /home/username/anaconda3, then:

pip install --target=/home/username/anaconda3/default_package_name

but, some indicate that this may not install properly via 'target' option...

or

2.  From here... "Use pip installed with conda, e.g. ~/anaconda/bin/pip. Use it to install packages into a conda environment, as well as to see the union of packages installed with this pip and with conda install."  But...

A.  to run pip from anaconda3, both from /home/username and /home/username/anaconda3/bin, if I type "which pip" it responds '/home/username/anaconda3/bin/pip'.  So, I presume conda's 'pip' will be used (no risk of using wrong pip...).  But, I also find that my $PATH is prepended (by my install of anaconda):

/home/username/anaconda3/bin:/home/username/bin:/home/username/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

and every attempt I make to run the Ubuntu python fails (one example only is shown below):

username@MyComputer:/usr/bin$ python3.5
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

I can find python2.7 and python3.5 under /usr/bin/ and some appear to be links, others not:

lrwxrwxrwx  1 root root           9 Sep  7 16:14 python -> python2.7
lrwxrwxrwx  
1 root root           9 Sep  7 16:14 python2 -> python2.7
-rwxr-xr-x  1 root root     3546104 Jul  2 14:05 python2.7
lrwxrwxrwx  
1 root root           9 Sep  7 16:14 python3 -> python3.5
-rwxr-xr-x  1 root root     4456272 Jul  7 04:17 python3.5
-rwxr-xr-x  1 root root     4456272 Jul  7 04:17 python3.5m
lrwxrwxrwx  
1 root root          10 Sep  7 16:14 python3m -> python3.5m


I presume I did not mess up Ubuntu python when I installed Anaconda, please confirm, but how would I run the non-Anaconda python?



B. will are the proper options with 'pip' to take are of it properly if I don't install via conda?



2. And regarding 'conda environment', can you help me understand what that means?  Does that simply mean to install matplotlib in the proper location per my question earlier, or is this something more specific/different?

Thanks

Tom Brander

unread,
Oct 24, 2016, 1:19:04 PM10/24/16
to Anaconda - Public
If you just follow the installation instructions for anaconda you will have matplotlib, and everything will be properly configured, read the docs...

You are making things way too complicated.. you do not need anything like what you are attempting..

harolds...@gmail.com

unread,
Jun 7, 2018, 8:31:37 AM6/7/18
to Anaconda - Public, rainer...@esak.de
This did the trick....thank for posting.
Reply all
Reply to author
Forward
0 new messages