我正在尝试使用两个2D数组绘制轮廓图(两个数组的形状均为(6,1),分别为x和y,而我的第三个数据(z)是x向量之间的计算出的RMS(x列表仅6个项)和y向量(y列表有600个项)。RMS值是每个点的标量。我的意思是我每个点都有一个RMS值。当我尝试使用x,y,z绘制等高线图时,以下错误出现:
TypeError:输入z必须为2D数组
如何解决这个问题?
预先感谢
import numpy
import matplotlib.pyplot as plt
from math import sqrt
def Cumulative_Sensitivity (depth, coil_spacing, coil_position):
global z
global s
z=numpy.array(depth)
s=numpy.array(coil_spacing)
if coil_position == "hcp":
cs=(4*(z/s)**2+1)**(-0.5)
cs=numpy.array(cs)
elif coil_position == "vcp":
cs=(4*(z/s)**2+1) **(0.5)-2*(z/s)
cs=numpy.array(cs)
return cs
def forward_model (sigma, depth, coil_spacing, coil_position):
global cs
global cond_true
global cond_apps
cond_true=numpy.array(sigma)
cond_apps=numpy.zeros(len(coil_spacing))
for i in range (0, len(coil_spacing)):
cs= Cumulative_Sensitivity (depth, coil_spacing[i], coil_position)
cond_app = sum(cond_true[:-1]*(cs[:-1]-cs[1:]))
cond_app = cond_app + cond_true[-1]*(cs[-1])
cond_apps[i] = cond_app
return cond_apps
# s1=30mS/m and s2=50mS/m, z=1m
sa_1=forward_model ([30, 50], [0, 1], [0.32], "hcp")
sa_2=forward_model ([30, 50], [0, 1], [0.71], "hcp")
sa_3=forward_model ([30, 50], [0, 1], [1.18], "hcp")
sa_4=forward_model ([30, 50], [0, 1], [0.32], "vcp")
sa_5=forward_model ([30, 50], [0, 1], [0.71], "vcp")
sa_6=forward_model ([30, 50], [0, 1], [1.18], "vcp")
data=numpy.array([sa_1, sa_2, sa_3, sa_4, sa_5, sa_6])
#
for i in range (10,110, 10):
for j in range (10,110, 10):
cond_HC1=forward_model ([j, i], [0, 1], [0.32], "hcp")
cond_HC2=forward_model ([j, i], [0, 1], [0.71], "hcp")
cond_HC3=forward_model ([j, i], [0, 1], [1.18], "hcp")
cond_VC1=forward_model ([j, i], [0, 1], [0.32], "vcp")
cond_VC2=forward_model ([j, i], [0, 1], [0.71], "vcp")
cond_VC3=forward_model ([j, i], [0, 1], [1.18], "vcp")
predicted=numpy.array([cond_HC1, cond_HC2, cond_HC3, cond_VC1, cond_VC2, cond_VC3])
rms=numpy.array(sqrt(sum((predicted - data)**2).mean()))
x = numpy.linspace(10,100,10)
y = numpy.linspace(10,100,10)
X,Y=numpy.meshgrid(x,y)
Z=rms
contour = plt.contour(X, Y, Z)