我正在为我的大学项目画一个球面谐波。我想描述的公式如下:

Y = cos(theta)

为此,我写了这段代码
import numpy as np
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

def sph2cart(r, phi, tta):
   ''' r is from 0 to infinity '''
   ''' phi is from 0 to 2*pi '''
   ''' tta is from 0 to pi '''
   x = r* np.sin(tta)* np.cos(phi)
   y = r* np.sin(tta)* np.sin(phi)
   z = r* np.cos(tta)
   return x, y, z

# phi running from 0 to pi and tta from 0 to pi
phi = np.linspace(0, 2* np.pi, 25)
tta = np.linspace(0, np.pi, 25)
# meshgrid to generate points
phi, tta = np.meshgrid(phi, tta)

# THIS IS THE FUNCTION
Y = np.cos(tta)
# finally all things in cartesian co-ordinate system
# Note that "Y" is acting as "r"
x, y, z = sph2cart( Y, phi, tta)

# plotting :-
fig = plt.figure()
ax = fig.add_subplot( 111 , projection='3d')
ax.plot_surface(x, y, z, linewidth = 0.5, edgecolors = 'k')

结果,得到球体。这是不正确的,因为实际的结果是哑铃状的形状。看这张图的第二行,
https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Spherical_Harmonics.png/1024px-Spherical_Harmonics.png

最佳答案

wikipedia文章中的图片Spherical harmonics是通过使用球谐函数的绝对值作为r坐标,然后根据谐函数的符号对曲面着色得到的。这是一个近似值。

x, y, z = sph2cart(np.abs(Y), phi, tta)

fig = plt.figure()
ax = fig.add_subplot( 111 , projection='3d')

from matplotlib import cm
ax.set_aspect('equal')
ax.plot_surface(x, y, z, linewidth = 0.5, facecolors = cm.jet(Y), edgecolors = 'k')

python - 在Python中可视化球谐函数-LMLPHP
当使用y本身作为r时,两个半球(正y和负y)最终映射到上述曲面的相同一半上。

07-25 20:53