Reputation: 532
I'm learning to work with the matplotlib animation function. I took the tutorial from matplotlib and changed a bit. So far I'm able to show my complete array (list) with length of 10. (see code)
My goal is to see in every frame only e.g. 3 values from the array at the same time. In the next frame the "window" should slide over with the stepsize of 1 Index so that the "old" values will move to the left and at the right are the "more new" values:
1st frame - see values list[3],list[2],list[1] from the array
2nd frame - see values list[4],list[3],list[2] from the array
3rd frame - see values list[5],list[4],list[3] from the array
....
Base Code from matplotlib tutorial, just a bit modified to show my complete array (list) but without updating.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
list = np.array([1,8,3,4,5,3,2,1,5,1])
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 10)
def update(data):
line.set_ydata(data)
return line,
def data_gen():
while True:
#yield np.random.rand(4)
yield list
ani = animation.FuncAnimation(fig, update, data_gen, interval=500)
plt.show()
I dont know how I can "slide" my window and then to update the animation. Probably with a for loop over the index of the frames I guess... ?
Which variable consists the index of the frames?
Thx a lot for your time. Have a nice weekend !
Upvotes: 2
Views: 3704
Reputation: 40697
I don't know what you were trying to do with your generator, but I think this is what you are trying to do?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
point_to_show = 3
my_list = np.array([1,8,3,4,5,3,2,1,5,1])
fig, ax = plt.subplots()
line, = ax.plot(range(point_to_show),np.zeros(point_to_show)*np.NaN, 'ro-')
ax.set_ylim(0, 10)
ax.set_xlim(0, point_to_show-1)
def update(i):
new_data = my_list[i:i+point_to_show]
line.set_ydata(new_data)
return line,
ani = animation.FuncAnimation(fig, update, frames=len(my_list)-point_to_show, interval=500)
plt.show()
Upvotes: 4