我正在寫一個2D FDTD光線傳播的程序,在這段代碼中,當我用ax.imshow()
命令在動畫函數中運行該程序時,程序工作正常,而當我使用im.set_data()
命令,它給了我一個空白的圖像。有人可以告訴我我做錯了什麼嗎?另外,有人可以告訴我如何在開始時設置顏色表,這樣我就不必在動畫循環中更新顏色表。關鍵是我不希望imshow()
命令每次運行循環時都會繪製所有內容。 感謝您的幫助。我正在學習編程,請告訴我該怎麼做。imshow.set_data()對FuncAnimation不起作用
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()
非常感謝! :) – DarthSpeedious 2014-10-24 16:27:19