【发布时间】: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')
然后,得到球体。这是不正确的,因为实际结果是哑铃状。看这张图片的第二行,
【问题讨论】:
-
它不是一个球体,它是一个椭球体,如果你想看到真正的比例使用
plt.axis("equal"),那个方程只是一个叶。 -
我还发现了this page,它绘制了一些漂亮的球谐函数。
标签: python python-3.x numpy