I'm trying to generate data for a matplotlib animation.
I have a data_gen function for matplotlib's "animation.FuncAnimation" function that is called like this:
ani = animation.FuncAnimation(fig, update, frames=data_gen, init_func=init, interval=10, blit=True)
My code has this form:
def func(a):
a += 1
return a
b = 0
def data_gen():
global b
c = func(b)
b = c
yield c
Unfortunately, this does not do what I want! For example,
print(data_gen().__next__())
print(data_gen().__next__())
print(data_gen().__next__())
for k in data_gen():
print(k)
... produces this output:
1
2
3
4
I was expecting that the for loop would run forever, but it does not. (It stops at 4.)
The behavior I need is:
(1) set initial value for b
(2) update b each time the generator runs
All suggestions greatly appreciated!