python06——行星绕轨(2D+3D)

题目:

代码:

2D

# -*- coding: utf-8 -*-

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.animation as animmation

#前处理
m = plt.figure()
axis = m.add_subplot(111)
axis.set_facecolor('black')
axis.set_xlim([0, 70])
axis.set_ylim([-70, 0])

#太阳属性的设定
sun = axis.plot([0], [0], color = 'red', markersize = 50, marker = 'o')

#转动物理量设定
r_rotate = [30, 40, 45, 50, 55, 60, 65, 70]
o_rotate = [15, 10, 8, 5, 4, 3, 2, 1]
t_limit = np.arange(0, 2.005, 0.005)
t_len = len(t_limit)

#坐标位置确定
x = []
y = []
z = np.zeros(t_len)
for i in range(len(r_rotate)):
    x.append(r_rotate[i] * np.cos(o_rotate[i] * np.pi * t_limit))
    y.append(r_rotate[i] * np.sin(o_rotate[i] * np.pi * t_limit))

#轨道绘制
for i in range(len(r_rotate)):
    axis.plot(x[i], y[i], 'gray', linewidth = 0.2)

#行星属性设定
ball1, = axis.plot([], [], color = 'yellow', markersize = 6, marker = 'o')
ball2, = axis.plot([], [], color = 'blue', markersize = 5, marker = 'o')
ball3, = axis.plot([], [], color = 'orange', markersize = 7, marker = 'o')
ball4, = axis.plot([], [], color = 'pink', markersize = 4, marker = 'o')
ball5, = axis.plot([], [], color = 'white', markersize = 8, marker = 'o')
ball6, = axis.plot([], [], color = 'green', markersize = 3, marker = 'o')
ball7, = axis.plot([], [], color = 'purple', markersize = 9, marker = 'o')
ball8, = axis.plot([], [], color = 'gray', markersize = 10, marker = 'o')
def __init__():
    return ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8
def update(data):
    ball1.set_data([data[0],data[1]])
    ball2.set_data([data[2],data[3]])
    ball3.set_data([data[4],data[5]])
    ball4.set_data([data[6],data[7]])
    ball5.set_data([data[8],data[9]])
    ball6.set_data([data[10],data[11]])
    ball7.set_data([data[12],data[13]])
    ball8.set_data([data[14],data[15]])
    return ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8

#运动方程
def equation():
    data = []
    for i in range(1, t_len):
        t = t_limit[i]
        x1 = r_rotate[0] * np.cos(o_rotate[0] * np.pi * t)
        y1 = r_rotate[0] * np.sin(o_rotate[0] * np.pi * t)
        x2 = r_rotate[1] * np.cos(o_rotate[1] * np.pi * t)
        y2 = r_rotate[1] * np.sin(o_rotate[1] * np.pi * t)
        x3 = r_rotate[2] * np.cos(o_rotate[2] * np.pi * t)
        y3 = r_rotate[2] * np.sin(o_rotate[2] * np.pi * t)
        x4 = r_rotate[3] * np.cos(o_rotate[3] * np.pi * t)
        y4 = r_rotate[3] * np.sin(o_rotate[3] * np.pi * t)
        x5 = r_rotate[4] * np.cos(o_rotate[4] * np.pi * t)
        y5 = r_rotate[4] * np.sin(o_rotate[4] * np.pi * t)
        x6 = r_rotate[5] * np.cos(o_rotate[5] * np.pi * t)
        y6 = r_rotate[5] * np.sin(o_rotate[5] * np.pi * t)
        x7 = r_rotate[6] * np.cos(o_rotate[6] * np.pi * t)
        y7 = r_rotate[6] * np.sin(o_rotate[6] * np.pi * t)
        x8 = r_rotate[7] * np.cos(o_rotate[7] * np.pi * t)
        y8 = r_rotate[7] * np.sin(o_rotate[7] * np.pi * t)
        data.append([x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8])
    return data
#调用FuncAnimation接口获取动态效果
anim=animmation.FuncAnimation(m, update, frames=equation(), init_func=__init__, interval=20)

3D

# -*- coding: utf-8 -*-

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.animation as animmation
from mpl_toolkits.mplot3d import Axes3D

#前处理
m = plt.figure(figsize = (15,15))
axis = m.add_subplot(111,projection = '3d')
axis.set_facecolor('black')
plt.axis('off')
axis.set_xlim([-50, 50])
axis.set_ylim([-50, 50])
axis.set_zlim([-3, 3])

#太阳属性的设定
sun = axis.plot([0], [0], [0], color = 'red', markersize = 50, marker = 'o')

#转动物理量设定
r_rotate = [30, 40, 45, 50, 55, 60, 65, 70]
o_rotate = [15, 10, 8, 5, 4, 3, 2, 1]
t_limit = np.arange(0, 2.005, 0.005)
t_len = len(t_limit)

#坐标位置确定
x = []
y = []
z = np.zeros(t_len)
for i in range(len(r_rotate)):
    x.append(r_rotate[i] * np.cos(o_rotate[i] * np.pi * t_limit))
    y.append(r_rotate[i] * np.sin(o_rotate[i] * np.pi * t_limit))

#轨道绘制
for i in range(len(r_rotate)):
    axis.plot(x[i], y[i], z, 'gray', linewidth = 0.2)

#行星属性设定
ball1, = axis.plot([], [], [], color = 'yellow', markersize = 6, marker = 'o')
ball2, = axis.plot([], [], [], color = 'blue', markersize = 5, marker = 'o')
ball3, = axis.plot([], [], [], color = 'orange', markersize = 7, marker = 'o')
ball4, = axis.plot([], [], [], color = 'pink', markersize = 4, marker = 'o')
ball5, = axis.plot([], [], [], color = 'white', markersize = 8, marker = 'o')
ball6, = axis.plot([], [], [], color = 'green', markersize = 3, marker = 'o')
ball7, = axis.plot([], [], [], color = 'purple', markersize = 9, marker = 'o')
ball8, = axis.plot([], [], [], color = 'gray', markersize = 10, marker = 'o')
def __init__():
    return ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8
def update(data):
    ball1.set_data([data[0],data[1]])
    ball1.set_3d_properties(data[2])
    ball2.set_data([data[3],data[4]])
    ball2.set_3d_properties(data[5])
    ball3.set_data([data[6],data[7]])
    ball3.set_3d_properties(data[8])
    ball4.set_data([data[9],data[10]])
    ball4.set_3d_properties(data[11])
    ball5.set_data([data[12],data[13]])
    ball5.set_3d_properties(data[14])
    ball6.set_data([data[15],data[16]])
    ball6.set_3d_properties(data[17])
    ball7.set_data([data[18],data[19]])
    ball7.set_3d_properties(data[20])
    ball8.set_data([data[21],data[22]])
    ball8.set_3d_properties(data[23])
    return ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8

#运动方程
def equation():
    data = []
    for i in range(1, t_len):
        t = t_limit[i]
        x1 = r_rotate[0] * np.cos(o_rotate[0] * np.pi * t)
        y1 = r_rotate[0] * np.sin(o_rotate[0] * np.pi * t)
        x2 = r_rotate[1] * np.cos(o_rotate[1] * np.pi * t)
        y2 = r_rotate[1] * np.sin(o_rotate[1] * np.pi * t)
        x3 = r_rotate[2] * np.cos(o_rotate[2] * np.pi * t)
        y3 = r_rotate[2] * np.sin(o_rotate[2] * np.pi * t)
        x4 = r_rotate[3] * np.cos(o_rotate[3] * np.pi * t)
        y4 = r_rotate[3] * np.sin(o_rotate[3] * np.pi * t)
        x5 = r_rotate[4] * np.cos(o_rotate[4] * np.pi * t)
        y5 = r_rotate[4] * np.sin(o_rotate[4] * np.pi * t)
        x6 = r_rotate[5] * np.cos(o_rotate[5] * np.pi * t)
        y6 = r_rotate[5] * np.sin(o_rotate[5] * np.pi * t)
        x7 = r_rotate[6] * np.cos(o_rotate[6] * np.pi * t)
        y7 = r_rotate[6] * np.sin(o_rotate[6] * np.pi * t)
        x8 = r_rotate[7] * np.cos(o_rotate[7] * np.pi * t)
        y8 = r_rotate[7] * np.sin(o_rotate[7] * np.pi * t)
        zi = 0
        data.append([x1, y1, zi, x2, y2, zi, x3, y3, zi, x4, y4, zi, x5, y5, zi, x6, y6, zi, x7, y7, zi, x8, y8, zi])
    return data
#调用FuncAnimation接口获取动态效果
anim=animmation.FuncAnimation(m, update, frames=equation(), init_func=__init__, interval=20)

Note:

* numpy的使用

* list列表的灵活运用

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值