Matplotlib FuncAnimation,当blit = true时出错。

时间:2016-01-28 17:45:28

标签: python animation matplotlib

我正在尝试使用FuncAnimation和circle.set_radius()制作扩展圆的动画。但是,动画仅在blit = False时有效。代码如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

fig, ax = plt.subplots()
plt.grid(True)
plt.axis([-0.6, 0.6, -0.6, 0.6])
circle1= plt.Circle([0,0],0.01,color="0.",fill=False, clip_on = True)
ax.add_patch(circle1)

dt = 1.0/20
vel = 0.1

def init():
    circle1.set_radius(0.01)

def animate(i):
    global dt, vel
    r = vel * i * dt
    circle1.set_radius(r)
    return circle1,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval= 50, blit=True)

它返回此错误:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/figure.py", line 1139, in draw
    self.canvas.draw_event(renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1809, in draw_event
    self.callbacks.process(s, event)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/cbook.py", line 562, in process
    proxy(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/cbook.py", line 429, in __call__
    return mtd(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/animation.py", line 620, in _start
    self._init_draw()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/animation.py", line 1166, in _init_draw
    for a in self._drawn_artists:
TypeError: 'NoneType' object is not utterable

我正在使用mac OS。当我改变blit = False时,动画将起作用。但是,每当我移动鼠标时,动画都会变慢。这是有问题的,因为我有一个单独的线程生成声音输出。在实践中,圆圈将击中一些数据点并发出声音。结果他们不同步。请帮忙。

1 个答案:

答案 0 :(得分:1)

From the docs

  

如果blit = True,func和init_func应该返回一个可重复的drawables来清除。

因此 - 您需要将return circle1,添加到您的函数init()。另一个选择是在您对init_func的通话中根本不指定FuncAnimation - 您可能不需要它。没有它,动画可能会做你想要的。

注意 circle1之后的尾随逗号 - 这意味着返回一个(1个元素)元组,以便返回值可根据需要进行迭代。您已在animate函数中使用此功能。