在 Python 中制作一个旋转球体

Make a Rotating Sphere in Python

当我试图对恒星脉动模式建模时,我已经制作了以球面方式应用球面谐波的代码。理想情况下,我希望能够将旋转的图像保存为 gif 图像。我找到了几个执行此操作的代码示例,但其中 none 似乎适用于我的代码或使用了我无法使用的 python 包。我不确定这是否超出了我 python 的技能范围,因为我是一个初学者。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
from scipy.special import sph_harm     #import package to calculate spherical harmonics

theta = np.linspace(0, 2*np.pi, 100)   #setting range for theta
phi = np.linspace(0, np.pi, 100)       #setting range for phi
phi, theta = np.meshgrid(phi, theta)   #setting the grid for phi and theta

#Setting the cartesian coordinates of the unit sphere
#Converting phi, theta, z to cartesian coordinates
x = np.sin(phi)*np.cos(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(phi)

m, l = 4, 4   #m and l control the mode of pulsation and overall appearance of the figure

#Calculating the spherical harmonic Y(l,m) and normalizing it
figcolors = sph_harm(m, l, theta, phi).real
figmax, figmin = figcolors.max(), figcolors.min()
figcolors = (figcolors-figmin)/(figmax-figmin)

#Setting the aspect ratio to 1 which makes the sphere look spherical and not elongated
fig = plt.figure(figsize=plt.figaspect(1.))    #aspect ratio
axes = fig.add_subplot(111, projection='3d')   #sets figure to 3d

#Sets the plot surface and colors of the figure where seismic is the color scheme
axes.plot_surface(x, y, z,  rstride=1, cstride=1,  facecolors=cm.autumn(figcolors))
#yellow zones are cooler and compressed, red zones are warmer and expanded

#Turn off the axis planes so only the sphere is visible
axes.set_axis_off()
fig.suptitle('m=4   l=4', fontsize=18, x=0.52, y=.85)

plt.savefig('m4_l4.png')          #saves a .png file of my figure
plt.show()                        #Plots the figure

#figure saved for m=1, 2, 3, 4 and l=2, 3, 5, 6 respectively then all 6 were put together to form a single figure

我还有一张图片显示我的代码当前输出的内容。当然,它只是一个静止的球体。先感谢您! sphere4_4

更改代码的最后一部分以生成一组数字(见下文)。在这种情况下,我创建 num = 10 帧,您可以根据需要更改此数字。然后打开一个终端并输入

convert m4_l4*.png m4_l4.gif

这是结果

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
from scipy.special import sph_harm     #import package to calculate spherical harmonics

theta = np.linspace(0, 2*np.pi, 100)   #setting range for theta
phi = np.linspace(0, np.pi, 100)       #setting range for phi
phi, theta = np.meshgrid(phi, theta)   #setting the grid for phi and theta

#Setting the cartesian coordinates of the unit sphere
#Converting phi, theta, z to cartesian coordinates
x = np.sin(phi)*np.cos(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(phi)

m, l = 4, 4   #m and l control the mode of pulsation and overall appearance of the figure

#Calculating the spherical harmonic Y(l,m) and normalizing it
figcolors = sph_harm(m, l, theta, phi).real
figmax, figmin = figcolors.max(), figcolors.min()
figcolors = (figcolors-figmin)/(figmax-figmin)

#Setting the aspect ratio to 1 which makes the sphere look spherical and not elongated
fig = plt.figure(figsize=plt.figaspect(1.))    #aspect ratio
axes = fig.add_subplot(111, projection='3d')   #sets figure to 3d

#Sets the plot surface and colors of the figure where seismic is the color scheme
axes.plot_surface(x, y, z,  rstride=1, cstride=1,  facecolors=cm.autumn(figcolors))
#yellow zones are cooler and compressed, red zones are warmer and expanded

axes.set_axis_off()
fig.suptitle('m=4   l=4', fontsize=18, x=0.52, y=.85)

for idx, angle in enumerate(np.linspace(0, 360, 10)):

    axes.view_init(30, angle)
    plt.draw()

    #Turn off the axis planes so only the sphere is visible


    plt.savefig('m4_l4-%04d.png' % idx)          #saves a .png file of my figure
plt.show()