用Python制作一个旋转球体

用Python制作一个旋转球体,python,animation,3d,gif,Python,Animation,3d,Gif,我制作了这个代码,在我试图模拟恒星脉动模式时,它以球形的方式应用了球面谐波。理想情况下,我希望能够有一个旋转的图像,可以保存为gif图像。我已经找到了一些这样做的代码示例,但似乎没有一个适用于我的代码或使用我不可用的python包。我不确定这是否超出了我在python方面的技能范围,因为我是个初学者 import numpy as np import matplotlib.pyplot as plt from matplotlib import cm, colors from mpl_toolk

我制作了这个代码,在我试图模拟恒星脉动模式时,它以球形的方式应用了球面谐波。理想情况下,我希望能够有一个旋转的图像,可以保存为gif图像。我已经找到了一些这样做的代码示例,但似乎没有一个适用于我的代码或使用我不可用的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

我还得到了一个显示我的代码当前输出的图像。当然,它只是一个静止的球体。提前谢谢你

更改代码的最后一部分以生成一组图形(见下文)。在本例中,我创建了
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()