Reputation: 365
I have code that generates graphs of a propagating gausian. Each plot plt.plot(r, Sevol[0])
generates an image of the type:
Many subplots at the same figure generetes the image above:
plt.plot(r, Sevol[0])
plt.plot(r, Sevol[10])
plt.plot(r, Sevol[20])
plt.plot(r, Sevol[30])
plt.plot(r, Sevol[40])
I want to make an animation where I can plot each image generated by one plt.plot(r, Sevol[i])
at a time, so that the previous one is superimposed by the next one.
fig = plt.figure()
plt.xlabel('r')
plt.xlabel('S')
plt.grid()
#plt.plot(r, Sevol[0])
#plt.plot(r, Sevol[2])
#plt.plot(r, Sevol[3])
#plt.plot(r, Sevol[4], color='C1', linestyle='--', linewidth=1)
#plt.plot(r, Sevol[5], color='C0', linestyle='--', linewidth=1)
#plt.plot(r, Sevol[6], color='C1', linestyle='--', linewidth=1)
plt.xlim(ri, rf)
graph, = plt.plot([], [], color='gold', markersize=3)
def animate(i):
graph.set_data(r[:i], Sevol[:i])
return graph,
skipframes = int(len(r)/200)
if skipframes == 0:
skipframes = 1
ani = FuncAnimation(fig, animate, frames=range(0,len(r),skipframes), interval=10, blit = True, repeat = False)
ani
Upvotes: 1
Views: 242
Reputation: 773
Instead of using the animate(i)
function to return the graph every frame, you can just call plt.plot()
for each of the lines you are trying to display.
Removing the skipframes
variable/loop can also make it a bit more clear what the frames
value in FuncAnimation
is supposed to be.
NOTE: This isn't the most efficient approach, but as long as you are plotting relatively few samples you won't have any lagging issues in the animation.
#################### DELETE THIS #######################
graph, = plt.plot([], [], color='gold', markersize=3)
def animate(i):
graph.set_data(r[:i], yLst[:i])
return graph,
skipframes = int(len(r)/200)
if skipframes == 0:
skipframes = 1
ani = FuncAnimation(fig, animate, frames=range(0,len(r),skipframes), interval=10, blit = True, repeat = False)
#################### INSERT THIS #######################
def animate(i):
numPlots = i //10 # how many plots (-1) will be shown based on the frame.
for n in range(numPlots):
plt.plot(r[n], Sevol[n], color='gold', markersize=3)
ani = FuncAnimation(fig, animate, frames=100, interval=10, blit = True, repeat = False)
Upvotes: 1