如何在matplotlib中绘制f(x,y,z)(即4D数据)的函数

2024-09-28 19:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将高斯光束的强度绘制为x、y和z的函数,但是到目前为止,我在这个网站上没有找到其他答案,只能将单个z切片绘制为等高线图。我知道绘图应该是什么样的(即中间最强烈的颜色,随着z,r的增加而衰减),但无法取得进展。我附上了强度生成函数和当前绘图的图像

def w0calc(s, f, w0dash, wavelength):
    zr = (np.pi*(w0dash**2))/wavelength
    w0 = w0dash/np.sqrt(((1 - (s/f))**2) + ((zr/f)**2))
    return w0

wavelength = 0.0004*1000 # 400nm in mm
w0val = w0calc(1.8*1000, 1.8*1000, 3*1000, wavelength)
w0val

def onephotonintensity3d(z, x, y, w0, wavelength):
    Izr = np.zeros([len(z), len(x), len(y)])
    for i in np.arange(len(z)):
        r = np.sqrt(np.square(x) + np.square(y))
        wz = w0*np.sqrt(1 + np.square((wavelength*z[i])/(np.pi*np.square(w0))))
        wzsqr = np.square(wz)
        Izr[i, :, :] = np.multiply(np.divide(np.square(w0), wzsqr), np.exp(np.divide(-2*np.square(r), wzsqr)))
    return Izr

x = np.linspace(-w0val*6, w0val*6, 100)
y = np.linspace(-w0val*6, w0val*6, 100)
z = np.linspace(-w0val*6, w0val*6, 100)
[X, Y] = np.meshgrid(x, y)
Intvals = onephotonintensity3d(z, X, Y, w0val, wavelength)

fig = plt.figure() % at present best solution, non-ideal
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Intvals[50, :, :], 50, cmap='viridis')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Intensity')

Current "best" plot


Tags: lennp绘制sqrtaxsetsquare强度