间隔增加时动画图形闭合(FuncAnimation)/ FuncAnimation中的间隔说明

时间:2014-10-03 06:19:34

标签: python-2.7 matplotlib

我正在尝试为一维FDTD波传播编写一个程序,除了FuncAnimation的interval关键字参数外,一切都很好。每当我将间隔从10增加(精确到19)时,动画图形会在运行前关闭(弹出后立即退出)。现在我可以使用time.sleep轻松减慢动画速度,但如果我能理解这一点就会很棒。有人可以解释一下这个区间参数是如何工作的。它是否与函数所需的时间有关,它更新了FuncAnimation正在调用的帧?什么是blit?

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

def main():

    #defining dimensions
    xdim=720
    time_tot = 500
    xsource = xdim/2

    #stability factor
    S=1

    #Speed of light
    c=1
    epsilon0=1
    mu0=1

    delta =1                                # Space step
    deltat = S*delta/c                      # Time step

    Ez = np.zeros(xdim)                     # Arrays to store Electric field and magnetic field
    Hy = np.zeros(xdim)

    epsilon = epsilon0*np.ones(xdim)        #Permittivity and permeability values.
    mu = mu0*np.ones(xdim)                  

    fig , axis = plt.subplots(1,1)
    axis.set_xlim(len(Ez))
    axis.set_ylim(-3,3)
    axis.set_title("E Field")
    line, = axis.plot([],[])

    def init():
        line.set_data([],[])
        return line,

    def animate(n, *args, **kwargs):
        Hy[0:xdim-1] = Hy[0:xdim-1]+(delta/(delta*mu[0:xdim-1]))*(Ez[1:xdim]-Ez[0:xdim-1])
        Ez[1:xdim]= Ez[1:xdim]+(delta/(delta*epsilon[1:xdim]))*(Hy[1:xdim]-Hy[0:xdim-1])
        #Ez[xsource] = Ez[xsource] + 30.0*(1/np.sqrt(2*np.pi))*np.exp(-(n-80.0)**2/(100))
        Ez[xsource]=np.sin(2*n*np.pi/180)
        ylims = axis.get_ylim()
        if (abs(np.amax(Ez))>ylims[1]):              # Scaling axis
            axis.set_ylim(-(np.amax(Ez)+2),np.amax(Ez)+2)
        line.set_data(np.arange(len(Ez)),Ez)
        return line,

    ani = animation.FuncAnimation(fig, animate, init_func=init, frames=(time_tot), interval=10, blit=False, repeat =False)
    fig.show()

if __name__ == "__main__": main()

0 个答案:

没有答案