簡體   English   中英

Matplotlib設置背景顏色和矢量化FDTD方程

[英]Matplotlib setting background color and vectorizing FDTD equation

我目前在使用Matplotlib時遇到了一些麻煩。 我有一個FDTD程序在運行時被“褪色”,因為圖像的背景顏色似乎是平均的。 我想將它全部設置為黑色(數組的0值)。 我該怎么做呢? 在Matplotlib的網站上發現了這個 ,但是當我嘗試它時它不起作用(它一直告訴我它沒想到色彩映射的字節)。

另外:有沒有辦法進一步矢量化while循環? 我正在考慮創建一個“掩碼”數組的行,這些值將指示值是否得到評估。 試圖創建另一個索引對我大喊大叫,以便轉換為多個值。

碼:

# -*- 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之前,我從未將該編碼行放在頂部。 為什么現在需要這個?

用這個:

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

防止它使用數組中的最低值作為colormap中的最低值。 如果要在每個步驟中保持相同的色彩映射,還有vmax參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM