2014-10-22 45 views
2

我正在寫一個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() 

回答

1

其實,你的第一個版本也工作得很好。問題在於,因爲im使用零數組進行初始化,色標的vminvmax都是零。更新至im之後,使用set_data未更新vminvmax,而ax.imshow自動重新調整顏色範圍。如果年初設定的東西合理的顏色範圍,它工作正常:

ax.imshow(Hz, vmin=-0.2, vmax=0.2) 

這就是你需要從代碼中的問題,改變的唯一的事情,使之(在動畫功能與im.set_data)工作。

+0

非常感謝! :) – DarthSpeedious 2014-10-24 16:27:19

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()