|
发表于 2020-8-8 18:00:03
|
显示全部楼层
- #引入第三方库
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- import numpy as np
- #建立三维数据
- θ=np.arange(0,np.pi,0.1)
- φ=np.arange(0,2*np.pi,0.1)
- θ,φ = np.meshgrid(θ,φ)
- #球函数字典
- dic1={
- 0:np.sin(φ),
- 1:(np.cos(θ))*(np.sin(φ)),
- 2:((3*(np.cos(2*θ))+1)/4)*(np.sin(φ))
- }
- dic2={
- 1:(np.cos(θ))*(np.sin(φ)),
- 2:3*(np.sin(θ))*(np.cos(θ))*(np.sin(φ)),
- 4:3*(np.sin(θ))*(np.sin(θ))*(np.sin(φ))
- }
- #定义三维坐标轴
- fig=plt.figure()
- ax=Axes3D(fig)
- #球函数的阶数
- def sp_main(t):
- if t==0:
- Y=dic1.get(t)
- Y= np.maximum(Y, 0)
-
- else:
- Y=dic2.get(t)
- Y= np.maximum(Y, 0)
- return Y
- def main():
- l=input('请输入阶数l:')
- m=input('请输入m:')
- x = int(l)
- y = int(m)
- t=x*y
- Y= sp_main(t)
- #作图
- plt.rcParams['font.sans-serif']=['SimHei']
- ax.set_xlabel('仰角θ')
- ax.set_ylabel('方位角φ')
- ax.set_zlabel('球函数Y')
- ax.set_xlim(0,np.pi)
- ax.set_ylim(0,2*np.pi)
- ax.set_zlim(0,1)
- ax.plot_surface(θ, φ, Y)
- plt.show()
- if __name__ == "__main__":
- main()
复制代码 |
|