- import numpy as np
- import matplotlib as mpl
- mpl.use(“TkAgg”)
- from matplotlib import pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D#使用mpl_toolkits.mplot3d下Axes3D绘制3D图形
- import matplotlib.animation as animmation
- r1=5
- r2=8
- r3=11
- r4=16
- r5=20
- r6=27
- r7=36
- r8=50#设置轨道半径
- omega1 = 13* np.pi
- omega2 = 9* np.pi
- omega3 = 7* np.pi
- omega4 = 6* np.pi
- omega5 = 5* np.pi
- omega6 = 4* np.pi
- omega7 = 3* np.pi
- omega8 = 2* np.pi#设置行星角速度
- def update(data):#随时间t的变换设置新的行星位置坐标,也即是随着animmation.FuncAnimation函数的帧数参数变化每一帧的不同坐标位置的图片对象参数
-
global line1,line2,line3,line4,line5,line6,line7,line8
-
line1.set_data([data[0], data[1]])
-
line1.set_3d_properties(data[2])
-
line2.set_data([data[3],data[4]])
-
line2.set_3d_properties(data[5])
-
line3.set_data([data[6], data[7]])
-
line3.set_3d_properties(data[8])
-
line4.set_data([data[9], data[10]])
-
line4.set_3d_properties(data[11])
-
line5.set_data([data[12], data[13]])
-
line5.set_3d_properties(data[14])
-
line6.set_data([data[15], data[16]])
-
line6.set_3d_properties(data[17])
-
line7.set_data([data[18], data[19]])
-
line7.set_3d_properties(data[20])
-
line8.set_data([data[21], data[22]])
-
line8.set_3d_properties(data[23])
-
return line1,line2,line3,line4,line5,line6,line7,line8
- def init():#行星的起始位置
-
global line1, line2, line3,line4,line5,line6,line7,line8
-
ti = 0
-
t = t_drange[np.mod(ti, t_dlen)]
-
xt1 = x0 + r1 * np.cos(omega1 * t)
-
yt1 = y0 + r1 * np.sin(omega1 * t)
-
zt1 = z0 + 0
-
xt2 = x0 + r1 * np.cos(omega2 * t)
-
yt2 = y0 + r1 * np.sin(omega2 * t)
-
zt2 = z0 + 0
-
xt3 = x0 + r1 * np.cos(omega3 * t)
-
yt3 = y0 + r1 * np.sin(omega3 * t)
-
zt3 = z0 + 0
-
xt4 = x0 + r1 * np.cos(omega4 * t)
-
yt4 = y0 + r1 * np.sin(omega4 * t)
-
zt4 = z0 + 0
-
xt5 = x0 + r1 * np.cos(omega5 * t)
-
yt5 = y0 + r1 * np.sin(omega5 * t)
-
zt5 = z0 + 0
-
xt6 = x0 + r1 * np.cos(omega6 * t)
-
yt6 = y0 + r1 * np.sin(omega6 * t)
-
zt6 = z0 + 0
-
xt7 = x0 + r1 * np.cos(omega7 * t)
-
yt7 = y0 + r1 * np.sin(omega7 * t)
-
zt7 = z0 + 0
-
xt8 = x0 + r1 * np.cos(omega8 * t)
-
yt8 = y0 + r1 * np.sin(omega8 * t)
-
zt8 = z0 + 0
-
line1, = ax.plot([xt1], [yt1], [zt1], marker='o', color='yellow',markersize=4)
-
line2, = ax.plot([xt2], [yt2], [zt2], marker='o', color='yellow',markersize=4)
-
line3, = ax.plot([xt3], [yt3], [zt3], marker='o', color='blue',markersize=4)
-
line4, = ax.plot([xt4], [yt4], [zt4], marker='o', color='red',markersize=4)
-
line5, = ax.plot([xt5], [yt5], [zt5], marker='o', color='brown',markersize=10)
-
line6, = ax.plot([xt6], [yt6], [zt6], marker='o', color='blue',markersize=10)
-
line7, = ax.plot([xt7], [yt7], [zt7], marker='o', color='blue',markersize=8)
-
line8, = ax.plot([xt8], [yt8], [zt8], marker='o', color='green',markersize=8)
-
return line1,line2, line3,line4,line5,line6,line7,line8
- def data_gen():#随时间t变换的行星坐标,也即是animmation.FuncAnimation函数的帧数参数
-
global x0,y0,z0,t_dlen
-
#while true:
-
data = []
-
for ti in range(1,t_dlen):
-
t = t_drange[ti]
-
#行星与太阳坐标的关系方程
-
xt1 = x0 + r1 * np.cos(omega1 * t)
-
yt1 = y0 + r1 * np.sin(omega1 * t)
-
zt1 = z0
-
xt2 = x0 + r2 * np.cos(omega2 * t)
-
yt2 = y0 + r2 * np.sin(omega2 * t)
-
zt2 = z0
-
xt3 = x0 + r3 * np.cos(omega3 * t)
-
yt3 = y0 + r3 * np.sin(omega3 * t)
-
zt3 = z0
-
xt4 = x0 + r4 * np.cos(omega4 * t)
-
yt4 = y0 + r4 * np.sin(omega4 * t)
-
zt4 = z0
-
xt5 = x0 + r5 * np.cos(omega5 * t)
-
yt5 = y0 + r5 * np.sin(omega5 * t)
-
zt5 = z0
-
xt6 = x0 + r6 * np.cos(omega6 * t)
-
yt6 = y0 + r6 * np.sin(omega6 * t)
-
zt6 = z0
-
xt7 = x0 + r7 * np.cos(omega7 * t)
-
yt7 = y0 + r7 * np.sin(omega7 * t)
-
zt7 = z0
-
xt8 = x0 + r8 * np.cos(omega8 * t)
-
yt8 = y0 + r8 * np.sin(omega8 * t)
-
zt8 = z0
-
data.append([xt1,yt1,zt1,xt2,yt2,zt2,xt3,yt3,zt3,xt4,yt4,zt4,xt5,yt5,zt5,xt6,yt6,zt6,xt7,yt7,zt7,xt8,yt8,zt8])
-
return data
- t_range = np.arange(0, 1 + 0.005, 0.005)#设置环绕一周时间的范围以及运动间隔时间
- t_drange = np.arange(0, 1, 0.005 )
- t_len = len(t_range)
- t_dlen = len(t_drange)
- #sun’s coordination
- x0 = 0
- y0 = 0
- z0 = 0
- #行星坐标随时间变化方程
- #水星
- x1 = x0 + r1 * np.cos(omega1 * t_range)
- y1 = y0 + r1 * np.sin(omega1 * t_range)
- z1 = z0 + np.zeros(t_len)
- #金星
- x2 = x0 + r2 * np.cos(omega2 * t_range)
- y2 = y0 + r2 * np.sin(omega2 * t_range)
- z2 = z0 + np.zeros(t_len)
- #地球
- x3 = x0 + r3 * np.cos(omega3 * t_range)
- y3 = y0 + r3 * np.sin(omega3 * t_range)
- z3 = z0 + np.zeros(t_len)
- #火星
- x4 = x0 + r4 * np.cos(omega4 * t_range)
- y4 = y0 + r4 * np.sin(omega4 * t_range)
- z4 = z0 + np.zeros(t_len)
- #木星
- x5 = x0 + r5 * np.cos(omega5 * t_range)
- y5 = y0 + r5 * np.sin(omega5 * t_range)
- z5 = z0 + np.zeros(t_len)
- #土星
- x6 = x0 + r6 * np.cos(omega6 * t_range)
- y6 = y0 + r6 * np.sin(omega6 * t_range)
- z6 = z0 + np.zeros(t_len)
- #天王星
- x7 = x0 + r7 * np.cos(omega7 * t_range)
- y7 = y0 + r7 * np.sin(omega7 * t_range)
- z7 = z0 + np.zeros(t_len)
- #海王星
- x8 = x0 + r8 * np.cos(omega8 * t_range)
- y8 = y0 + r8 * np.sin(omega8 * t_range)
- z8 = z0 + np.zeros(t_len)
- f = plt.figure(figsize=(12,12))#绘图的画布
- ax = f.add_subplot(111,projection=‘3d’)#设置3d坐标系
- ax.set_title(“Sun Model”)#设置图像标题
- ax.plot([0], [0], [0], marker=‘o’, color= ‘red’, markersize=10)#设置太阳的属性
- ax.plot(x1, y1, z1, ‘r’)
- ax.plot(x2, y2, z2, ‘r’)
- ax.plot(x3, y3, z3, ‘r’)
- ax.plot(x4, y4, z4, ‘r’)
- ax.plot(x5, y5, z5, ‘r’)
- ax.plot(x6, y6, z6, ‘r’)
- ax.plot(x7, y7, z7, ‘r’)
- ax.plot(x8, y8, z8, ‘r’)#绘制行星的图像
- ax.set_xlim([-(r8 + 10), (r8 + 10)])
- ax.set_ylim([-(r8 + 10), (r8 + 10)])
- ax.set_zlim([-5, 5])#模型在坐标系中的范围
- line1, = ax.plot([], [], [], marker=‘o’, color=‘yellow’,markersize=4,animated = True)
- line2, = ax.plot([], [], [], marker=‘o’, color=‘yellow’,markersize=4,animated = True)
- line3, = ax.plot([], [], [], marker=‘o’, color=‘blue’,markersize=4,animated = True)
- line4, = ax.plot([], [], [], marker=‘o’, color=‘red’,markersize=4,animated = True)
- line5, = ax.plot([], [], [], marker=‘o’, color=‘brown’,markersize=10,animated = True)
- line6, = ax.plot([], [], [], marker=‘o’, color=‘blue’,markersize=10,animated = True)
- line7, = ax.plot([], [], [], marker=‘o’, color=‘blue’,markersize=8,animated = True)
- line8, = ax.plot([], [], [], marker=‘o’, color=‘green’,markersize=8,animated = True)#行星的形状颜色及大小等属性
- #r
- ani = animmation.FuncAnimation(f, update, frames = data_gen(), init_func = init,interval = 200)
- #f#将上述函数对象传入animmation.FuncAnimation函数以生成连续的行星运动模型
- plt.show()
python太阳系行星动态演示
猜你喜欢
转载自blog.csdn.net/qq_43518269/article/details/105742590
今日推荐
周排行