python太阳系行星动态演示

  1. import numpy as np
  2. import matplotlib as mpl
  3. mpl.use(“TkAgg”)
  4. from matplotlib import pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D#使用mpl_toolkits.mplot3d下Axes3D绘制3D图形
  6. import matplotlib.animation as animmation
  7. r1=5
  8. r2=8
  9. r3=11
  10. r4=16
  11. r5=20
  12. r6=27
  13. r7=36
  14. r8=50#设置轨道半径
  15. omega1 = 13* np.pi
  16. omega2 = 9* np.pi
  17. omega3 = 7* np.pi
  18. omega4 = 6* np.pi
  19. omega5 = 5* np.pi
  20. omega6 = 4* np.pi
  21. omega7 = 3* np.pi
  22. omega8 = 2* np.pi#设置行星角速度
  23. def update(data):#随时间t的变换设置新的行星位置坐标,也即是随着animmation.FuncAnimation函数的帧数参数变化每一帧的不同坐标位置的图片对象参数
  24. global line1,line2,line3,line4,line5,line6,line7,line8  
    
  25. line1.set_data([data[0], data[1]])  
    
  26. line1.set_3d_properties(data[2])  
    
  27. line2.set_data([data[3],data[4]])  
    
  28. line2.set_3d_properties(data[5])  
    
  29. line3.set_data([data[6], data[7]])  
    
  30. line3.set_3d_properties(data[8])  
    
  31. line4.set_data([data[9], data[10]])  
    
  32. line4.set_3d_properties(data[11])  
    
  33. line5.set_data([data[12], data[13]])  
    
  34. line5.set_3d_properties(data[14])  
    
  35. line6.set_data([data[15], data[16]])  
    
  36. line6.set_3d_properties(data[17])  
    
  37. line7.set_data([data[18], data[19]])  
    
  38. line7.set_3d_properties(data[20])  
    
  39. line8.set_data([data[21], data[22]])  
    
  40. line8.set_3d_properties(data[23])  
    
  41. return line1,line2,line3,line4,line5,line6,line7,line8  
    
  42. def init():#行星的起始位置
  43. global line1, line2, line3,line4,line5,line6,line7,line8  
    
  44. ti = 0  
    
  45. t = t_drange[np.mod(ti, t_dlen)]  
    
  46. xt1 = x0 + r1 * np.cos(omega1 * t)  
    
  47. yt1 = y0 + r1 * np.sin(omega1 * t)  
    
  48. zt1 = z0 + 0  
    
  49. xt2 = x0 + r1 * np.cos(omega2 * t)  
    
  50. yt2 = y0 + r1 * np.sin(omega2 * t)  
    
  51. zt2 = z0 + 0  
    
  52. xt3 = x0 + r1 * np.cos(omega3 * t)  
    
  53. yt3 = y0 + r1 * np.sin(omega3 * t)  
    
  54. zt3 = z0 + 0  
    
  55. xt4 = x0 + r1 * np.cos(omega4 * t)  
    
  56. yt4 = y0 + r1 * np.sin(omega4 * t)  
    
  57. zt4 = z0 + 0  
    
  58. xt5 = x0 + r1 * np.cos(omega5 * t)  
    
  59. yt5 = y0 + r1 * np.sin(omega5 * t)  
    
  60. zt5 = z0 + 0  
    
  61. xt6 = x0 + r1 * np.cos(omega6 * t)  
    
  62. yt6 = y0 + r1 * np.sin(omega6 * t)  
    
  63. zt6 = z0 + 0  
    
  64. xt7 = x0 + r1 * np.cos(omega7 * t)  
    
  65. yt7 = y0 + r1 * np.sin(omega7 * t)  
    
  66. zt7 = z0 + 0  
    
  67. xt8 = x0 + r1 * np.cos(omega8 * t)  
    
  68. yt8 = y0 + r1 * np.sin(omega8 * t)  
    
  69. zt8 = z0 + 0  
    
  70. line1, = ax.plot([xt1], [yt1], [zt1], marker='o', color='yellow',markersize=4)  
    
  71. line2, = ax.plot([xt2], [yt2], [zt2], marker='o', color='yellow',markersize=4)  
    
  72. line3, = ax.plot([xt3], [yt3], [zt3], marker='o', color='blue',markersize=4)  
    
  73. line4, = ax.plot([xt4], [yt4], [zt4], marker='o', color='red',markersize=4)  
    
  74. line5, = ax.plot([xt5], [yt5], [zt5], marker='o', color='brown',markersize=10)  
    
  75. line6, = ax.plot([xt6], [yt6], [zt6], marker='o', color='blue',markersize=10)  
    
  76. line7, = ax.plot([xt7], [yt7], [zt7], marker='o', color='blue',markersize=8)  
    
  77. line8, = ax.plot([xt8], [yt8], [zt8], marker='o', color='green',markersize=8)  
    
  78. return line1,line2, line3,line4,line5,line6,line7,line8  
    
  79. def data_gen():#随时间t变换的行星坐标,也即是animmation.FuncAnimation函数的帧数参数
  80. global x0,y0,z0,t_dlen  
    
  81. #while true:  
    
  82. data = []  
    
  83. for ti in range(1,t_dlen):  
    
  84.     t = t_drange[ti]  
    
  85.     #行星与太阳坐标的关系方程  
    
  86.     xt1 = x0 + r1 * np.cos(omega1 * t)  
    
  87.     yt1 = y0 + r1 * np.sin(omega1 * t)  
    
  88.     zt1 = z0  
    
  89.     xt2 = x0 + r2 * np.cos(omega2 * t)  
    
  90.     yt2 = y0 + r2 * np.sin(omega2 * t)  
    
  91.     zt2 = z0   
    
  92.     xt3 = x0 + r3 * np.cos(omega3 * t)  
    
  93.     yt3 = y0 + r3 * np.sin(omega3 * t)  
    
  94.        zt3 = z0   
    
  95.        xt4 = x0 + r4 * np.cos(omega4 * t)  
    
  96.        yt4 = y0 + r4 * np.sin(omega4 * t)  
    
  97.        zt4 = z0   
    
  98.        xt5 = x0 + r5 * np.cos(omega5 * t)  
    
  99.        yt5 = y0 + r5 * np.sin(omega5 * t)  
    
  100.        zt5 = z0   
    
  101.        xt6 = x0 + r6 * np.cos(omega6 * t)  
    
  102.        yt6 = y0 + r6 * np.sin(omega6 * t)  
    
  103.        zt6 = z0   
    
  104.        xt7 = x0 + r7 * np.cos(omega7 * t)  
    
  105.        yt7 = y0 + r7 * np.sin(omega7 * t)  
    
  106.        zt7 = z0   
    
  107.        xt8 = x0 + r8 * np.cos(omega8 * t)  
    
  108.        yt8 = y0 + r8 * np.sin(omega8 * t)  
    
  109.        zt8 = z0   
    
  110.        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])  
    
  111.    return data  
    
  112. t_range = np.arange(0, 1 + 0.005, 0.005)#设置环绕一周时间的范围以及运动间隔时间
  113. t_drange = np.arange(0, 1, 0.005 )
  114. t_len = len(t_range)
  115. t_dlen = len(t_drange)
  116. #sun’s coordination
  117. x0 = 0
  118. y0 = 0
  119. z0 = 0
  120. #行星坐标随时间变化方程
  121. #水星
  122. x1 = x0 + r1 * np.cos(omega1 * t_range)
  123. y1 = y0 + r1 * np.sin(omega1 * t_range)
  124. z1 = z0 + np.zeros(t_len)
  125. #金星
  126. x2 = x0 + r2 * np.cos(omega2 * t_range)
  127. y2 = y0 + r2 * np.sin(omega2 * t_range)
  128. z2 = z0 + np.zeros(t_len)
  129. #地球
  130. x3 = x0 + r3 * np.cos(omega3 * t_range)
  131. y3 = y0 + r3 * np.sin(omega3 * t_range)
  132. z3 = z0 + np.zeros(t_len)
  133. #火星
  134. x4 = x0 + r4 * np.cos(omega4 * t_range)
  135. y4 = y0 + r4 * np.sin(omega4 * t_range)
  136. z4 = z0 + np.zeros(t_len)
  137. #木星
  138. x5 = x0 + r5 * np.cos(omega5 * t_range)
  139. y5 = y0 + r5 * np.sin(omega5 * t_range)
  140. z5 = z0 + np.zeros(t_len)
  141. #土星
  142. x6 = x0 + r6 * np.cos(omega6 * t_range)
  143. y6 = y0 + r6 * np.sin(omega6 * t_range)
  144. z6 = z0 + np.zeros(t_len)
  145. #天王星
  146. x7 = x0 + r7 * np.cos(omega7 * t_range)
  147. y7 = y0 + r7 * np.sin(omega7 * t_range)
  148. z7 = z0 + np.zeros(t_len)
  149. #海王星
  150. x8 = x0 + r8 * np.cos(omega8 * t_range)
  151. y8 = y0 + r8 * np.sin(omega8 * t_range)
  152. z8 = z0 + np.zeros(t_len)
  153. f = plt.figure(figsize=(12,12))#绘图的画布
  154. ax = f.add_subplot(111,projection=‘3d’)#设置3d坐标系
  155. ax.set_title(“Sun Model”)#设置图像标题
  156. ax.plot([0], [0], [0], marker=‘o’, color= ‘red’, markersize=10)#设置太阳的属性
  157. ax.plot(x1, y1, z1, ‘r’)
  158. ax.plot(x2, y2, z2, ‘r’)
  159. ax.plot(x3, y3, z3, ‘r’)
  160. ax.plot(x4, y4, z4, ‘r’)
  161. ax.plot(x5, y5, z5, ‘r’)
  162. ax.plot(x6, y6, z6, ‘r’)
  163. ax.plot(x7, y7, z7, ‘r’)
  164. ax.plot(x8, y8, z8, ‘r’)#绘制行星的图像
  165. ax.set_xlim([-(r8 + 10), (r8 + 10)])
  166. ax.set_ylim([-(r8 + 10), (r8 + 10)])
  167. ax.set_zlim([-5, 5])#模型在坐标系中的范围
  168. line1, = ax.plot([], [], [], marker=‘o’, color=‘yellow’,markersize=4,animated = True)
  169. line2, = ax.plot([], [], [], marker=‘o’, color=‘yellow’,markersize=4,animated = True)
  170. line3, = ax.plot([], [], [], marker=‘o’, color=‘blue’,markersize=4,animated = True)
  171. line4, = ax.plot([], [], [], marker=‘o’, color=‘red’,markersize=4,animated = True)
  172. line5, = ax.plot([], [], [], marker=‘o’, color=‘brown’,markersize=10,animated = True)
  173. line6, = ax.plot([], [], [], marker=‘o’, color=‘blue’,markersize=10,animated = True)
  174. line7, = ax.plot([], [], [], marker=‘o’, color=‘blue’,markersize=8,animated = True)
  175. line8, = ax.plot([], [], [], marker=‘o’, color=‘green’,markersize=8,animated = True)#行星的形状颜色及大小等属性
  176. #r
  177. ani = animmation.FuncAnimation(f, update, frames = data_gen(), init_func = init,interval = 200)
  178. #f#将上述函数对象传入animmation.FuncAnimation函数以生成连续的行星运动模型
  179. plt.show()
发布了2 篇原创文章 · 获赞 0 · 访问量 457

猜你喜欢

转载自blog.csdn.net/qq_43518269/article/details/105742590