繁体   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