FuncAnimation为长动画增加FPS?

问题描述 投票:0回答:1

我正在努力将我的C程序“翻译”为python。它是y轴上粒子的阻尼简谐运动。动画使用matplotlib中的FuncAnimation。它对每个步骤(帧)使用小步长(dh),以便精确地表示粒子的路径。小dh的问题,它增加了每单位距离的帧数。有没有办法增加每秒帧数,以便动画更快(更高的fps)小dh?

顺便说一下,该程序使用2D数组进行未来改进,例如使用多个粒子。但是,我还没有采用一种方法来动画所有粒子,但这不是问题。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

#The bungee rope has to have a [k = 0.15] in order to do 5 oscillations
N = 1
dt = 0.1 #//size step
time = 8000
count =1;
b = 1;
k = 0.2;
m= 100;

# function to open an x-window
fig = plt.figure()

#define positions and velocities for each
x = np.zeros((N,time))
y = np.zeros((N,time))
vx = np.zeros((N,time))
vy = np.zeros((N,time))

#define variables for the plotting canvas
xmin = -5.0; xmax =5.0;
ymin = -80.0; ymax = 80.0;

ax = plt.axes(xlim= (xmin,xmax), ylim=(ymin,ymax),ylabel = 'Y Position')
(my_point,) = ax.plot([],[],'ro',ms=7) #stored var to make red circle (ro) point


#set initial position and velocity
x[:N,0] = 0; y[:N,0] = 75;
vx[:N,0] = 0; vy[:N,0] = 0.0;

for i in range(time-1):
    #set path/function
    for j in range(N):
        a = (-k*y[j,i] - b*vy[j,i])/(m); #damped simple harmonic motion
        vy[j,i+1] = vy[j,i] +  a*dt #differential changes to velocity
        y[j,i+1] = y[j,i] + dt*vy[j,i+1] #differential changes to distance

def get_step(n,x,y,point):
    point.set_data(x[n],y[n])

mymovie = animation.FuncAnimation(fig,get_step,frames= time,\
                                  fargs=(x[0,:],y[0,:],my_point), interval = 0)   

plt.show()
print("For 5 oscillations k = %f\n" % k)
python animation matplotlib
1个回答
0
投票

动画的interval已经设置为0,理论上理论上每秒有无限数量的帧。实际上,这意味着动画的绘制速度与计算机允许的速度一样快。

动画中的限制因素是,对于每个帧,画布中的所有内容都会重新绘制。这是一个很大的开销,因为轴,刻度,标签等在动画期间都保持不变。克服这种限制的标准技术是blitting。这只会在每一帧中重新绘制动画艺术家(红球)。

def get_step(n,x,y,point):
    point.set_data(x[n],y[n])
    return point,

mymovie = animation.FuncAnimation(fig,get_step,frames= time, blit=True,\
                                  fargs=(x[0,:],y[0,:],my_point), interval = 1)

如果这还不够快,您可以考虑跳过动画的步骤,例如只显示每一步,

frames = time[::2]  
© www.soinside.com 2019 - 2024. All rights reserved.