【问题标题】:Visualizing spherical harmonics in Python在 Python 中可视化球谐函数
【发布时间】:2018-03-07 18:02:18
【问题描述】:

我正在尝试为我的大学项目绘制球谐函数。下面我要描绘的公式,

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

【问题讨论】:

  • 它不是一个球体,它是一个椭球体,如果你想看到真正的比例使用plt.axis("equal"),那个方程只是一个叶。
  • 我还发现了this page,它绘制了一些漂亮的球谐函数。

标签: python python-3.x numpy


【解决方案1】:

维基百科文章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')

当您将 Y 本身用作 r 时,两个半球(正 Y 和负 Y)最终映射到上述表面的同一半。

【讨论】:

    【解决方案2】:

    您传递给函数的 Y 必须是绝对值才能使其成为 r,否则 z = cos(theta)^2 始终为正。如果 r 是半径,那么这就是你应该做的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      相关资源
      最近更新 更多