我正在嘗試生成matplotlib動畫的數據。
我有matplotlib的「animation.FuncAnimation」功能的data_gen函數被調用是這樣的:matplotlib動畫的生成器函數
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) set initial value for b
(2) update b each time the generator runs
所有建議,不勝感激!
仍然不會工作,因爲在發電機中沒有環路。它只會產生一次。 – M4rtini
@ M4rtini你的權利,被其他問題分散注意力,並被全球國家所迷惑...... – tacaswell
@tcaswell這是非常有幫助的;但是,包括data_gen的參數在animation.FuncAnimation函數調用中似乎不起作用。如果我能縮小麻煩的根源,我會再問另一個問題。再次感謝。 – Riccati