木星及其卫星的3D交互式可视化动画
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
from matplotlib.patches import Circle
# 创建木星及其卫星的数据
data = {
'Jupiter': {
'radius': 69911,
'color': 'orange',
'orbit_radius': 0,
'orbit_speed': 0,
},
'Io': {
'radius': 1821.6,
'color': 'gray',
'orbit_radius': 421.6,
'orbit_speed': 17,
},
'Europa': {
'radius': 1560.8,
'color': 'lightblue',
'orbit_radius': 671.1,
'orbit_speed': 13,
},
'Ganymede': {
'radius': 2631.2,
'color': 'lightyellow',
'orbit_radius': 1070.4,
'orbit_speed': 10,
},
'Callisto': {
'radius': 2410.3,
'color': 'lightgreen',
'orbit_radius': 1882.7,
'orbit_speed': 8,
},
}
def interactive_visualization():
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
satellites = []
for name, satellite in data.items():
radius = satellite['radius']
color = satellite['color']
orbit_radius = satellite['orbit_radius']
orbit_speed = satellite['orbit_speed']
# 绘制卫星
satellite_obj = ax.scatter(orbit_radius, 0, 0, c=color, s=radius, label=name)
satellites.append(satellite_obj)
# 绘制卫星轨道
orbit = Circle((0, 0), orbit_radius, fill=False)
ax.add_patch(orbit)
ax.set_xlim([-3000, 3000])
ax.set_ylim([-3000, 3000])
ax.set_zlim([-3000, 3000])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# 添加交互操作
def update_orbits(frame):
for satellite, (name, satellite_data) in zip(satellites, data.items()):
orbit_radius = satellite_data['orbit_radius']
orbit_speed = satellite_data['orbit_speed']
# 更新卫星位置
angle = orbit_speed * frame
x = orbit_radius * 1.5 * 0.8 * (1.5 * 0.8) ** angle
y = orbit_radius * 0.8 * (0.8) ** angle
satellite._offsets3d = (np.array([x]), np.array([y]), np.array([0]))
# 动画效果
frames = 100
anim = FuncAnimation(fig, update_orbits, frames=frames, interval=50, repeat=True)
plt.legend()
plt.show()
anim.save("a.gif")
# 运行交互式可视化界面
interactive_visualization()
这段代码使用 Matplotlib 库创建了一个 3D 交互式动画,展示了木星及其卫星的轨道运动。该动画展示了卫星围绕木星的运动轨迹,以及卫星的大小和颜色。
您可以通过以下步骤运行该代码:
- 确保您的电脑上已经安装了 Python 和 Matplotlib 库。
- 将代码保存为一个 .py 文件(例如,jupiter_satellites.py)。
- 打开命令行窗口,并进入保存代码的目录。
- 运行命令
python jupiter_satellites.py。
代码运行后,将会弹出一个窗口,展示木星及其卫星的 3D 动画。您可以在窗口中交互式地调整视角,以更直观地观察动画。
代码说明:
- 代码首先创建了一个字典
data,包含木星及其卫星的数据,包括半径、颜色、轨道半径和轨道速度。 - 然后,代码使用 Matplotlib 创建了一个 3D 图形,并绘制了木星和卫星的初始位置。
- 接着,代码使用
FuncAnimation函数创建了一个动画,该动画会不断更新卫星的位置,从而模拟卫星围绕木星的运动。 update_orbits函数用于更新每个卫星的位置,该函数使用三角函数计算每个卫星在每个时间点的坐标。- 最后,代码保存动画为一个 GIF 文件。
如何修改动画:
- 您可以修改
data字典,添加或删除卫星,或修改卫星的属性,例如颜色、半径、轨道半径和轨道速度。 - 您也可以修改
update_orbits函数,以更改卫星的运动轨迹。 - 此外,您可以调整动画的帧数、间隔时间以及其他参数,以获得最佳的视觉效果。
希望这能帮到您!
原文地址: https://www.cveoy.top/t/topic/o25x 著作权归作者所有。请勿转载和采集!