我为Windows 7安装了Anaconda 3(64位) Python 3.4,并尝试测试Matplotlib中的一个示例。但当我运行脚本时,它出现了一个异常,如下所示:
Traceback (most recent call last):
File "<ipython-input-7-7482c0850da6>", line 1, in <module>
runfile('E:/Kanbox/Python/HWV/test/matplotlib_test.py', wdir='E:/Kanbox/Python/HWV/test')
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 48, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "E:/Kanbox/Python/HWV/test/matplotlib_test.py", line 36, in <module>
canvas.show()
File "C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 349, in draw
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
File "C:\Anaconda3\lib\site-packages\matplotlib\backends\tkagg.py", line 20, in blit
tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError代码来自here,未经修改的示例:
#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
def destroy(e): sys.exit()
root = Tk.Tk()
root.wm_title("Embedding in TK")
root.bind("<Destroy>", destroy)
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
a.plot(t,s)
a.set_title('Tk embedding')
a.set_xlabel('X axis label')
a.set_ylabel('Y label')
# A tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
#toolbar = NavigationToolbar2TkAgg(canvas, root)
#toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
button = Tk.Button(master=root, text='Quit', command=sys.exit)
button.pack(side=Tk.BOTTOM)
Tk.mainloop()似乎tkagg.blit无法获得正确的渲染器,所以它引发了一个异常。我找不到self.renderer._renderer提到的地方。然后我在spyderlib问题1831中发现了一个类似的问题:https://code.google.com/p/spyderlib/issues/detail?id=1831。
我猜这是Spyder之间的Python 3.4的问题,所以我为Windows 7安装了Anaconda (32位) Python 2.7,并尝试在另一个Windows 7系统中运行上面的示例脚本。然后tkinter GUI正常显示matplotlib图形,没有出现异常。所以我想也许这确实是Spyder版本的问题。我们的项目基于Python3.4,我们不想迁移回Python2.7,因为它迁移起来很复杂。我该如何解决这个问题?
发布于 2014-09-21 13:38:48
这是Anaconda tk Matplotlib后端的一个bug,据我所知它只影响Windows用户。
我让Continuum的人知道了这件事,但不幸的是,他们告诉我这对他们来说是一个低优先级的事情,因为很少有人使用tk后端。
https://stackoverflow.com/questions/25321841
复制相似问题