如何更改帧之间的间隔(python)? [英] How do I change the interval between frames (python)?
问题描述
所以我正在使用matplotlib.animation用python制作动画,我希望每个帧之间的时间在每个帧上都改变.根据到目前为止我发现的所有内容,时间间隔" arg只能是一个我希望将其作为数组的int.到目前为止的代码:
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()
基本上我想要第 i 帧的 interval=ti[i].(是的,我知道我的代码效率低下,我是 python 的新手,我只是从一堆网站上把它拖到一起)另外,如果这是不可能的,是否有一种简单的方法可以在事后编辑电影以达到相同的效果?
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?
推荐答案
你可以修改ani.event_source.interval
,如下代码
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,
警告:它似乎与GIF文件不兼容.
Warning: it seems not compatible with GIF file.
这篇关于如何更改帧之间的间隔(python)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!