用Python制作旋转球体

时间:2018-12-05 22:16:36

标签: python animation 3d gif

我制作了此代码,以便在我试图对恒星脉动模式建模时以球形方式应用球形谐波。理想情况下,我希望能够旋转的图像可以另存为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

我还有一张图像,显示了我的代码当前输出的内容。当然,这只是一个静止的领域。先感谢您! sphere4_4

1 个答案:

答案 0 :(得分:3)

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

convert m4_l4*.png m4_l4.gif

这就是结果

enter image description here

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