【发布时间】:2013-12-19 21:21:33
【问题描述】:
我正在尝试为 matplotlib 动画生成数据。
我有一个用于 matplotlib 的“animation.FuncAnimation”函数的 data_gen 函数,调用如下:
ani = animation.FuncAnimation(fig, update, frames=data_gen, init_func=init, interval=10, blit=True)
我的代码有这种形式:
def func(a):
a += 1
return a
b = 0
def data_gen():
global b
c = func(b)
b = c
yield c
不幸的是,这并没有达到我想要的效果!例如,
print(data_gen().__next__())
print(data_gen().__next__())
print(data_gen().__next__())
for k in data_gen():
print(k)
...产生这个输出:
1
2
3
4
我原以为 for 循环会永远运行,但事实并非如此。 (它停在 4 点。)
我需要的行为是:
(1) 为b设置初始值
(2) 每次生成器运行时更新 b
非常感谢所有建议!
【问题讨论】:
标签: python animation matplotlib generator