在Python中可视化球谐函数 [英] Visualizing spherical harmonics in Python

查看:973
本文介绍了在Python中可视化球谐函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的大学项目绘制球形谐波.我想描述以下公式,

I am trying to draw a spherical harmonics for my college project. The following formula I want to depict,

Y = cos(theta)

为此,我编写了这段代码

for that, I wrote this code

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')

然后得到球体.这是不正确的,因为实际结果是哑铃状的形状.看到这张图片的第二行,

And, get the sphere as a result. Which is not correct, because actual result is dumbbell like shape. See the second row of this image,

https://upload .wikimedia.org/wikipedia/commons/thumb/6/62/Spherical_Harmonics.png/1024px-Spherical_Harmonics.png

推荐答案

Wikipedia文章中的图片球形谐波是通过将球形谐波的绝对值作为r坐标,然后根据谐波的符号对表面着色而获得的.这是一个近似值.

The picture in the Wikipedia article Spherical harmonics is obtained by using the absolute value of a spherical harmonic as the r coordinate, and then coloring the surface according to the sign of the harmonic. Here is an approximation.

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)最终映射到上表面的同一半部.

When you use Y itself as r, the two hemispheres (positive Y and negative Y) end up mapped onto the same half of the above surface.

这篇关于在Python中可视化球谐函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆