我是python编程的新手。我试图通过修改matplotlib网站上的示例来显示表面图。你能告诉我什么是错的吗?
我的代码是:
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy
def H(n, f, l, delta, H_abs, H_ph):
c0 = 2.99796e8
n0 = 1.00027 + 0j
n1 = complex(n[0], n[1])
Sum = 0
for i in range(1, delta+1):
Sum = Sum + ((n0-n1)*exp(complex(0,-1*2*pi*f*n1*l/c0))/(n1+n0))**i
H_Theo = 4*n0*n1*exp(complex(0,2*pi*f*l)*(n0-n1)/c0)*(1+Sum)/(n0+n1)
M = abs(H_Theo) - H_abs
A = cmath.phase(H_Theo) - H_ph
return abs(M) + abs(A)
freq = 1213188709257.4814
l_real = 6.77e-4
d = 0
H_abs = 0.798653104536778
H_ph = 2.1845744701729926
n_test = numpy.arange(1.5, 2.5, 0.01).tolist()
k_test = numpy.arange(-0.05, 0.05, 0.001).tolist()
X = n_test
Y = k_test
X, Y = numpy.meshgrid(X, Y)
errore = []
for i in range(len(n_test)):
errore.append(H([X[i],Y[i]], freq, l_real, d, H_abs, H_ph))
Z = errore
fig2 = plt.figure()
az = fig2.gca(projection='3d')
az.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = az.contour(X, Y, Z, zdir='z', offset=min(Z)-1, cmap=cm.coolwarm)
cset = az.contour(X, Y, Z, zdir='x', offset=min(X)-1, cmap=cm.coolwarm)
cset = az.contour(X, Y, Z, zdir='y', offset=max(Y)+0.05, cmap=cm.coolwarm)
az.set_xlabel('n')
az.set_xlim(min(X)-1, max(X)+1)
az.set_ylabel('k')
az.set_ylim(min(Y)-0.05, max(Y)+0.05)
az.set_zlabel('Err')
az.set_zlim(min(Z)-1, max(Z)+1)
plt.show()
我得到的错误是:
n1 = complex(n[0], n[1])
TypeError: only length-1 arrays can be converted to Python scalars
我已经看过关于这个主题的其他问答,但无论如何我无法解决问题。
也许问题是微不足道的,但我真的很感激任何帮助,以便能够看到最终应该与此类似的图表:http://matplotlib.org/examples/mplot3d/contour3d_demo3.html
答案 0 :(得分:2)
你的问题是X [i]和Y [i]是1-d数组,你传入H作为n。即,n = [X [i],Y [i]]。然后你调用复数(n [0],n [1]),它需要标量参数,而不是数组。如果您希望n是1-d复数数组,则可以写n = n [0] + 1j * n [1]。但是,您仍需要修改其余代码。你错过了一些进口,似乎假设“来自numpy import *”
无论如何,我已经做了足够的修改以使其运行并绘制某些东西,但我真的怀疑这是你正在寻找的东西。你需要考虑一下你试图绘制的功能。
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy
def H(n, f, l, delta, H_abs, H_ph):
c0 = 2.99796e8
n0 = 1.00027 + 0j
n1 = n[0] + 1j * n[1]
Sum = 0
for i in range(1, delta+1):
Sum = Sum + ((n0-n1)*numpy.exp(-1*2*numpy.pi*f*n1*l/c0)/(n1+n0))**i
H_Theo = 4*n0*n1*numpy.exp(2*numpy.pi*f*l*(n0-n1)/c0)*(1+Sum)/(n0+n1)
M = numpy.abs(H_Theo) - H_abs
A = numpy.angle(H_Theo) - H_ph
return abs(M) + abs(A)
freq = 1213188709257.4814
l_real = 6.77e-4
d = 0
H_abs = 0.798653104536778
H_ph = 2.1845744701729926
n_test = numpy.arange(1.5, 2.5, 0.01).tolist()
k_test = numpy.arange(-0.05, 0.05, 0.001).tolist()
X = n_test
Y = k_test
X, Y = numpy.meshgrid(X, Y)
errore = []
for i in range(len(n_test)):
errore.append(H([X[i],Y[i]], freq, l_real, d, H_abs, H_ph))
Z = errore
fig2 = plt.figure()
az = fig2.gca(projection='3d')
az.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = az.contour(X, Y, Z, zdir='z', offset=numpy.min(Z)-1, cmap=cm.coolwarm)
cset = az.contour(X, Y, Z, zdir='x', offset=numpy.min(X)-1, cmap=cm.coolwarm)
cset = az.contour(X, Y, Z, zdir='y', offset=numpy.max(Y)+0.05, cmap=cm.coolwarm)
az.set_xlabel('n')
az.set_xlim(numpy.min(X)-1, numpy.max(X)+1)
az.set_ylabel('k')
az.set_ylim(numpy.min(Y)-0.05, numpy.max(Y)+0.05)
az.set_zlabel('Err')
az.set_zlim(numpy.min(Z)-1, numpy.max(Z)+1)
plt.show()