Matplotlib动画错误断言self._cachedRenderer不是None

时间:2014-03-16 16:04:32

标签: python animation matplotlib

我目前正在尝试为简谐运动系统制作动画。它绘制了加速度,位移和速度图表,使用代码:

self.fig = plt.Figure()
self.canvas = FigureCanvasTkAgg(self.fig, master=master)
self.canvas.get_tk_widget().grid(in_=master, column=0,row=1)
self.ax = self.fig.add_subplot(111)
self.line, = self.ax.plot(self.x, self.y)
self.ani = animation.FuncAnimation(self.fig, self.animate, interval=0, blit=True)
self.root.mainloop()

这是在tkinter框架内。 self.x和self.y只是显示三个不同图形所需的不同数据集。计算完数据后,它会通过defualt初始显示位移图。然后,用户可以按加速度或速度按钮切换到同一位置的不同图形。

第一张图表显示正常,但是一旦用户点击其他按钮之一,就会显示不同的图表错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1482, in __call__
    return self.func(*args)
  File "C:\Python33\lib\tkinter\__init__.py", line 534, in callit
    func(*args)
  File "C:\Python33\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 141, in _on_timer
    TimerBase._on_timer(self)
  File "C:\Python33\lib\site-packages\matplotlib\backend_bases.py", line 1203, in _on_timer
    ret = func(*args, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 877, in _step
    still_going = Animation._step(self, *args)
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 736, in _step
    self._draw_next_frame(framedata, self._blit)
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 756, in _draw_next_frame
    self._post_draw(framedata, blit)
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 779, in _post_draw
    self._blit_draw(self._drawn_artists, self._blit_cache)
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 794, in _blit_draw
    a.axes.draw_artist(a)
  File "C:\Python33\lib\site-packages\matplotlib\axes.py", line 2099, in draw_artist
    assert self._cachedRenderer is not None
AssertionError

出现并停止程序正常工作。我可以做些什么来阻止此错误?

1 个答案:

答案 0 :(得分:0)

@Joe Kington,所以对于加速图,我会将按钮代码用作:

self.accelerationbutton = ttk.Button(self.Buttonframe, text="Acceleration-time", command=self.acceleration)

对于该功能,它将是:

    def acceleration(self, x, y):
        self.fig = plt.Figure()
        self.canvas = FigureCanvasTkAgg(self.fig, master=master)
        self.canvas.get_tk_widget().grid(in_=master, column=0,row=1)
        self.ax = self.fig.add_subplot(111)
        self.line, = self.ax.plot(x, y)
        self.ani = animation.FuncAnimation(self.fig, self.animate, interval=0, blit=True)
        self.root.mainloop()

我用来绘制每个不同图表的图表的代码与绘制的数据完全相同,是问题吗?