Skip to content Skip to sidebar Skip to footer

Animate With Variable Time

I have trajectory data where each vehicle has its own time to start. Each vehicle is a point in the animation. So, in the dataset, for each row there is coordinate point (x,y) alon

Solution 1:

The already mentioned FuncAnimation has a parameter frame that the animation function can use an index:

import matplotlib.pyplot as plt
import matplotlib.animation as anim

fig = plt.figure()

x=[20,23,25,27,29,31]
y=[10,12,14,16,17,19]
t=[2,9,1,4,3,9]

#create index list for frames, i.e. how many cycles each frame will be displayed
frame_t = []
for i, item in enumerate(t):
    frame_t.extend([i] * item)

def init():
    fig.clear()

#animation function
def animate(i): 
    #prevent autoscaling of figure
    plt.xlim(15, 35)
    plt.ylim( 5, 25)
    #set new point
    plt.scatter(x[i], y[i], c = "b")

#animate scatter plot
ani = anim.FuncAnimation(fig, animate, init_func = init, 
                         frames = frame_t, interval = 100, repeat = True)
plt.show()

Equivalently, you could store the same frame several time in the ArtistAnimation list. Basically the flipbook approach.

Sample output: enter image description here


Post a Comment for "Animate With Variable Time"