我通过调用使用matplotlib动画:
plot = animation.FuncAnimation(fig, update, frames=data_gen(a), init_func=init, interval=10, blit=True)
Run Code Online (Sandbox Code Playgroud)
这里,"a"是data_gen函数的初始值,如下所示:
data_gen(x)
old_x = x
while True:
new_x = func(old_x)
old_x = new_x
yield new_x
Run Code Online (Sandbox Code Playgroud)
此代码的目的是让data_gen在每次更新动画图时为new_x生成新值.
但是......这恰巧发生了:
animation.py 在FuncAnimation类的init()方法中抛出错误.
此代码中出现此问题:
elif iterable(frames):
self._iter_gen = lambda: iter(frames)
self.save_count = len(frames)
Run Code Online (Sandbox Code Playgroud)
错误是"TypeError:类型'生成器'的对象没有len()"
看起来data_gen是可迭代的,但它没有len().
以下是FuncAnimation类中init()方法的更多代码:
# 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 …
Run Code Online (Sandbox Code Playgroud)