4

So I'm making an animation in python using matplotlib.animation, and I want the time between each frame to change on every frame. According to everything I've found so far, the 'interval' arg can only be an int where I want it to be an array. Code so far:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import matplotlib.animation as animation
import time

data=np.loadtxt("coord_dump.dat")
r=(data[:,0]**2+data[:,2]**2)**0.5
distance=np.diff(r)
ti=np.abs(distance)*1e-9
tt=0

fig = plt.figure()
ax=plt.axes(xlim=(-2e16,2e16),ylim=(-1.5e16,1.5e16))
ax.set_xlabel('x(cm)')
ax.set_ylabel('y(cm)')

x1 = data[:,0]
y1 = data[:,2]
x2 = data[:,1]
y2 = data[:,3]
line1 = Line2D([], [], color='black')
line1a = Line2D([], [], color='black', marker='o', markersize=6)
line2= Line2D([], [], color='red')
line2a = Line2D([], [], color='red', marker='o', markersize=6)
ax.add_line(line1)
ax.add_line(line2)
ax.add_line(line1a)
ax.add_line(line2a)

def animate(i):

    line1.set_data(x1[:i], y1[:i])
    line1a.set_data(x1[i], y1[i])
    line2.set_data(x2[:i], y2[:i])
    line2a.set_data(x2[i], y2[i])
    lines =  [line1, line1a, line2, line2a]
    tt=ti[i]
    return lines,tt

def init():
    lines =  [line1, line1a, line2, line2a]
    for l in lines:
        l.set_data([], [])
    return lines,

ani = animation.FuncAnimation(fig,animate,frames=15000, interval=5+tt,init_func=init, blit=True)
print tt
ani.save('xy2.mp4')
plt.show()          

Basically I want interval=ti[i] for frame i. (yes I know my code is inefficient, I'm new to python and I just hobbled this together from a bunch of websites) Also if this is impossible, is there a simple way to edit the movie afterwards to achieve the same effect?

1 Answer 1

1

You can change ani.event_source.interval, like the following code

def animate(i):

    line1.set_data(x1[:i], y1[:i])
    line1a.set_data(x1[i], y1[i])
    line2.set_data(x2[:i], y2[:i])
    line2a.set_data(x2[i], y2[i])
    lines =  [line1, line1a, line2, line2a]
    tt=ti[i]
    ani.event_source.interval = tt
    return lines,

Warning: it seems not compatible with GIF file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.