Matplotlib设置背景颜色并矢量化FDTD方程
我目前在使用 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 之前,我从来没有必要将该代码行放在顶部。为什么现在有必要这样做?
I'm currently having some trouble with Matplotlib. I have an FDTD program that gets 'washed out' as it runs because the background color of the image seems to average. I would like to set it all to black (the 0 values of the array). How would I go about doing this? I found this on Matplotlib's website, but it doesn't work when I try it (it keeps telling me it didn't expect a byte for colormap).
Also: Is there any way to further vectorize the while loop? I was thinking something along the lines of creating an array of 'mask' values that would indicate whether or not the values get evaluated. Trying to create another index yells at me for casting to multiple values.
Code:
# -*- 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()
Also: I've never had to put that coding line at the top until I switched to Eclipse as my IDE. Why is this now necessary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此:
防止它使用数组中的最低值作为颜色图中的最低值。如果您想在每个步骤中保持相同的颜色图,还有 vmax 参数。
Use this:
to prevent it from using the lowest value in your array as the lowest value in the colormap. There's also the vmax parameter if you want to keep the same colormap through each step.