2011-04-15 120 views
0

我目前Matplotlib有一些麻煩。由於圖像的背景顏色似乎平均,我有一個FDTD程序在運行時被「洗掉」。我想將它全部設置爲黑色(數組的0值)。我會如何去做這件事?我發現this on Matplotlib's website,但它在我嘗試它時不起作用(它一直告訴我它並不期望colormap有一個字節)。Matplotlib設置背景顏色和向量化FDTD方程

此外:有什麼辦法可以進一步矢量化while循環嗎?我正在考慮創建一個'mask'值的數組,這些值將指示值是否被評估。試圖創建另一個索引大聲對我投擲多個值。

代碼:

# -*- coding: cp1252 -*- 
from numpy import * 
from math import * 
import matplotlib.pyplot as plt 

def fdtd(): 
    print 'Starting simulation.' 
    # Define constants and parameters 
    #mu0 = pi*4E-7 # pH/µm 
    #e0 = 8.854187E-12 # Picofarads/micron 
    e0 = 8.85418782E-6 
    mu0 = 1.256637061 
    c = 1/sqrt(mu0*e0) 

    # Simulation Parameters 
    cellsizeX = 100. #Size of Yee-cell x edge in microns 
    cellsizeY = 100. #Size of Yee-cell y edge in microns 
    numX = 200 #Number of cells in X direction 
    numY = 200 #Number of cells in Y direction 
    lengthX = cellsizeX * numX 
    lengthY = cellsizeY * numY 
    dx = cellsizeX 
    dy = cellsizeY 
    dt = 1/(c*sqrt(1/dx**2+1/dy**2)) 
    wavelength = 550E-9 #nm (green) 
    freq = c/wavelength 
    CEy = dt/(dx*mu0) 
    CEx = dt/(dy*mu0) 
    CHx = dt/(dy*e0) 
    CHy = dt/(dx*e0) 
    times = 1 
    y = 0 

    # Array creation 
    print 'Creating arrays' 
    E = zeros(shape=((2*numX+1),(2*numY+1))) 
    Ep = E.copy() 
    H = zeros(shape=(2*numX,2*numY)) 
    Hp = H.copy() 
    Elec = E.copy() 

    #Create indexes 
    index = arange(0,2*numX, 1) 
    xindex = arange(0, 2*numX-1, 2) 
    yindex = arange(0, 2*numY-1, 2) 
    print 'Entering simulation loop.' 
    while times <= 500: 
     y = 0 
     # Initial Conditions 
     if (times < 100): 
      E[numX-50:numX+50,numY-50:numY+50] = times 

     # Calculate H and E fields 
     while y < len(yindex): 
      Hp[xindex+1,yindex[y]+1] = H[xindex+1,yindex[y]+1] - CEy*(E[xindex+2,yindex[y]+1] - E[xindex,yindex[y]+1]) + CEx*(E[xindex+1,yindex[y]+2] - E[xindex+1,yindex[y]]) 
      Ep[xindex,yindex[y]+1] = E[xindex,yindex[y]+1] - CHy*(Hp[xindex+1,yindex[y]+1] - Hp[xindex-1, yindex[y]+1]) 
      Ep[xindex+1,yindex[y]] = E[xindex+1,yindex[y]] + CHx*(Hp[xindex+1, yindex[y]+1] - Hp[xindex+1,yindex[y]-1]) 
      y+=1 

     # Boundary Conditions 
     Ep[numX*2, :] = Ep[numX*2-1,:] 
     Ep[:,numY*2] = Ep[:,numY*2-1] 
     Ep[0,:] = Ep[1,:] 
     Ep[:,0] = Ep[:,1] 

     #Name switching 
     E, Ep, H, Hp = Ep, E, Hp, H 

     #Plotting and Saving 
     plt.imshow(E[:,:], cmap = 'spectral') 
     filename = str('PATH\%03d' % times) + '.png' 
     plt.savefig(filename) 
     plt.clf() 
     times += 1 

if __name__ == '__main__': 
    fdtd() 

另外:我從來沒有把該編碼線在頂部,直到我切換到Eclipse作爲我的IDE。爲什麼現在這是必要的?

+0

編碼行是必要的,因爲您在「#mu0 =」註釋行中有一個unicode字符。 – 2011-04-15 06:37:19

+0

我會打開一個關於矢量化的單獨問題。你有沒有嘗試過使用yindex(除去[y]和while循環)? – 2011-04-15 06:47:39

+0

對不起,我的建議刪除[Y]將無法正常工作。但是你應該看看numpy.meshgrid和http://docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html – 2011-04-15 06:54:37

回答

2

使用此:

plt.imshow(E[:,:], cmap = 'spectral', vmin=0) 

,以防止它從你的陣列作爲顏色表中的最低值,使用中的最低值。如果要通過每個步驟保留相同的顏色映射,還有vmax參數。

+0

謝謝你,這很好用。 – MercuryRising 2011-04-16 05:14:12