解决方案是A)预先生成所有数据并将其推送到列表中(如果你真的有有限数量的帧),即data_list = list(data_gen)B)从我的分支机构安装来修复这个:
PR #2634或C)猴子补丁matplotlib,它只是在运行时使用固定代码替换安装中的错误代码
C)是最有趣的;)
# copied directly from the proposed fix
def monkey_patch_init(self, fig, func, frames=None, init_func=None, fargs=None,
save_count=None, **kwargs):
if fargs:
self._args = fargs
else:
self._args = ()
self._func = func
# Amount of framedata to keep around for saving movies. This is only
# used if we don't know how many frames there will be: in the case
# of no generator or in the case of a callable.
self.save_count = save_count
# Set up a function that creates a new iterable when needed. If nothing
# is passed in for frames, just use itertools.count, which will just
# keep counting from 0. A callable passed in for frames is assumed to
# be a generator. An iterable will be used as is, and anything else
# will be treated as a number of frames.
if frames is None:
self._iter_gen = itertools.count
elif six.callable(frames):
self._iter_gen = frames
elif iterable(frames):
self._iter_gen = lambda: iter(frames)
if hasattr(frames, '__len__'):
self.save_count = len(frames)
else:
self._iter_gen = lambda: xrange(frames).__iter__()
self.save_count = frames
# If we're passed in and using the default, set it to 100.
if self.save_count is None:
self.save_count = 100
self._init_func = init_func
# Needs to be initialized so the draw functions work without checking
self._save_seq = []
TimedAnimation.__init__(self, fig, **kwargs)
# Need to reset the saved seq, since right now it will contain data
# for a single frame from init, which is not what we want.
self._save_seq = []
我们现在有一个函数monkey_patch_init,它(我声称)是固定代码.我们现在用这个函数替换bug __init__函数:
matplotlib.animation.FuncAnimation.__init__ = monkey_patch_init
你的动画应该有效.
ani = animation.FuncAnimation(fig, update, frames=data_gen(a), init_func=init, interval=10, blit=True)
作为旁注,请勿将plot用作变量名.很多人将pyplot批量导入他们的名称空间(例如ipython –pylab),其中有情节 – > matplotlib.pyplot.plot – > plt.gca().plot因此它使得你的代码更容易让某人感到困惑.