我为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版本的问题。我们的项目基于Python 3.4,我们不想回到Python 2.7,因为迁移很复杂。我该如何解决这个问题?
答案 0 :(得分:1)
这是Anaconda tk
Matplotlib后端的一个错误,据我所知它只会影响Windows用户。
我让Continuum的人知道它,但不幸的是他们告诉我这是他们的低优先级之一,因为很少有人使用tk
后端。