imshow.set_data()不适用于FuncAnimation

时间:2014-10-22 07:52:40

标签: python python-2.7 animation matplotlib scipy

我正在为2D FDTD光传播编写程序,在此代码中,当我在animate函数中使用ax.imshow()命令运行程序时,程序工作正常,而当我使用im.set_data()命令时,它给了我一个空白的图像。有人可以告诉我,我做错了什么?此外,有人可以告诉我如何在开始时设置色彩映射,以便我不必在动画循环期间更新它。关键是我不希望imshow()命令在每次循环运行时绘制所有内容。 谢谢你的帮助。我正在学习编程请告诉我该怎么做。

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


xdim = 100        
ydim = 100
epsilon = np.ones([xdim,ydim])*8.854187817*10**(-12)
mu = np.ones([xdim,ydim])*4*np.pi*10**(-7)
c = 299792458
delta = 10**-6
deltat =  delta/(c*(2**0.5))

Hz = np.zeros([xdim,ydim])
Ey = np.zeros([xdim,ydim])
Ex = np.zeros([xdim,ydim])


fig = plt.figure()
ax = plt.axes()
im = ax.imshow(Hz)

Hz[xdim/2,ydim/2]=1

def init():
    im.set_data(np.zeros(Hz.shape))
    return

def animate(n, *args, **kwargs):
    Ex[0:xdim-1,0:ydim-1]=Ex[0:xdim-1,0:ydim-1]+(deltat/(delta*mu[0:xdim-1,0:ydim-1]))*(Hz[1:xdim,0:ydim-1]-Hz[0:xdim-1,0:ydim-1])
    Ey[0:xdim-1,0:ydim-1]=Ey[0:xdim-1,0:ydim-1]-(deltat/(delta*mu[0:xdim-1,0:ydim-1]))*(Hz[0:xdim-1,1:ydim]-Hz[0:xdim-1,0:ydim-1])

    Hz[1:xdim,1:ydim]=Hz[1:xdim,1:ydim]+(deltat/(delta*epsilon[1:xdim,1:ydim]))*(Ex[1:xdim,1:ydim]-Ex[0:xdim-1,1:ydim]-Ey[1:xdim,1:ydim]+Ey[1:xdim,0:ydim-1])
    if(n==0):Hz[xdim/2,ydim/2]=0
    #im.set_data(Hz)                 
    ax.imshow(Hz)     # Delete this command and try running the program with the above command. 
    return

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

2 个答案:

答案 0 :(得分:1)

我通过做一些改变让程序工作,但我无法理解为什么它不按照我在问题中写的方式工作。这是我改变了:

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


xdim = 100
ydim = 100
epsilon = np.ones([xdim,ydim])*8.854187817*10**(-12)
mu = np.ones([xdim,ydim])*4*np.pi*10**(-7)
c = 299792458
delta = 10**-6
deltat =  delta/(c*(2**0.5))

Hz = np.zeros([xdim,ydim])
Ey = np.zeros([xdim,ydim])
Ex = np.zeros([xdim,ydim])



Hz[xdim/2,ydim/2]=1

def init():
    global fig, ax, im
    fig = plt.figure()
    ax = plt.axes()
    im = ax.imshow(Hz, cmap="jet")
    im.set_data(np.zeros(Hz.shape))
    return

def animate(n):
    Ex[0:xdim-1,0:ydim-1]=Ex[0:xdim-1,0:ydim-1]+(deltat/(delta*mu[0:xdim-1,0:ydim-1]))*(Hz[1:xdim,0:ydim-1]-Hz[0:xdim-1,0:ydim-1])
    Ey[0:xdim-1,0:ydim-1]=Ey[0:xdim-1,0:ydim-1]-(deltat/(delta*mu[0:xdim-1,0:ydim-1]))*(Hz[0:xdim-1,1:ydim]-Hz[0:xdim-1,0:ydim-1])

    Hz[1:xdim,1:ydim]=Hz[1:xdim,1:ydim]+(deltat/(delta*epsilon[1:xdim,1:ydim]))*(Ex[1:xdim,1:ydim]-Ex[0:xdim-1,1:ydim]-Ey[1:xdim,1:ydim]+Ey[1:xdim,0:ydim-1])
    if(n==0):Hz[xdim/2,ydim/2]=0
    im.set_data(Hz)                 
    return

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

答案 1 :(得分:1)

实际上,你的第一个版本也正常工作。问题在于,因为im使用零数组初始化,所以色阶的vminvmax都为零。之后使用im的{​​{1}}更新未更新set_datavmin,而vmax会自动重新调整颜色范围。如果您将开头的颜色范围设置为合理的值,则可以正常工作:

ax.imshow

这是您需要更改问题中的代码才能使其正常工作(在动画功能中使用ax.imshow(Hz, vmin=-0.2, vmax=0.2) )。