太阳系是一个受太阳引力约束在一起的恒星系统,包括太阳以及直接或间接围绕太阳运动的天体。
轨道环绕太阳的天体被分为三类:行星、矮行星、和太阳系小天体。
行星是环绕太阳且质量够大的天体。
这类天体:
1、有足够的质量使本身的形状成为球体;
2、有能力清空邻近轨道的小天体。
能成为行星的天体有8个:水星、金星、地球、火星、木星、土星、天王星和海王星。
使用 Plotly 绘图的太阳系3D 模型使用 Plotly Scatter3D 和 Plotly Surface 组合在一张图中创建整个太阳系。
其中Scatter3D 用于绘制轨道,Surface 用于绘制球体。
在此模型中,将创建四个执行某些任务的函数:
pip install plotly -i https://pypi.tuna.tsinghua.edu.cn/simple
import plotly.graph_objects as go
import numpy as np
import math
定义一个函数来完成创建球体的工作
def sphere(radius,color,dist=0):
# dist 告诉我们距太阳的距离
# set 设置角度
theta = np.linspace(0,2*np.pi,100)
phi = np.linspace(0,np.pi,100)
# 设置球体的坐标
x = dist radius * np.outer(np.cos(theta),np.sin(phi))
y = radius * np.outer(np.sin(theta),np.sin(phi))
z = radius * np.outer(np.ones(100),np.cos(phi))
# 设置跟踪
trace = go.Surface(x=x, y=y, z=z, colorscale = [[0,color],[1,color]],showscale = False)
return trace
定义一个函数来绘制土星周围的轨道和环
def orbit(dist,offset = 0,color="white",width = 2):
# dist 是距太阳的距离
# offset 允许我们改变圆的中心
# 初始化空列表以设置每个坐标
x = []
y = []
z = []
# 计算坐标
for i in range(0,361):
x = x [(round(np.cos(math.radians(i)),5)) * dist offset]
y = y [(round(np.sin(math.radians(i)),5)) * dist]
z = z [0]
trace = go.Scatter3d(x=x, y=y, z=z, marker=dict(size=0.1), line=dict(color=color,width=width))
return trace
为标注定义函数
def annot(x, z, txt, xancr='center'):
st = dict(showarrow=False, x=x, y=0, z=z, text=txt, xanchor=xancr, font=dict(color='white',size=12))
return st
定义一个绘制太阳系的主函数
# 行星直径(公里)
diameter_km = [200000, 4878, 12104, 12756, 6787, 142796, 120660, 51118, 48600]
# 行星直径相对于地球大小
diameter = [((i / 12756) * 2) for i in diameter_km]
# 距太阳的距离(百万公里)
distance_from_sun = [0, 57.9, 108.2, 149.6, 227.9, 778.6, 1433.5, 2872.5, 4495.1]
# 为太阳和行星创建球体
trace0 = sphere(diameter[0], '#ffff00', distance_from_sun[0]) # Sun
trace1 = sphere(diameter[1], '#87877d', distance_from_sun[1]) # Mercury
trace2 = sphere(diameter[2], '#d23100', distance_from_sun[2]) # Venus
trace3 = sphere(diameter[3], '#325bff', distance_from_sun[3]) # Earth
trace4 = sphere(diameter[4], '#b20000', distance_from_sun[4]) # Mars
trace5 = sphere(diameter[5], '#ebebd2', distance_from_sun[5]) # Jupiter
trace6 = sphere(diameter[6], '#ebcd82', distance_from_sun[6]) # Saturn
trace7 = sphere(diameter[7], '#37ffda', distance_from_sun[7]) # Uranus
trace8 = sphere(diameter[8], '#2500ab', distance_from_sun[8]) # Neptune
# Set up orbit traces
trace11 = orbit(distance_from_sun[1]) # Mercury
trace12 = orbit(distance_from_sun[2]) # Venus
trace13 = orbit(distance_from_sun[3]) # Earth
trace14 = orbit(distance_from_sun[4]) # Mars
trace15 = orbit(distance_from_sun[5]) # Jupiter
trace16 = orbit(distance_from_sun[6]) # Saturn
trace17 = orbit(distance_from_sun[7]) # Uranus
trace18 = orbit(distance_from_sun[8]) # Neptune
# To draw a few rings for Saturn
trace21 = orbit(23, distance_from_sun[6], '#827962', 3)
trace22 = orbit(24, distance_from_sun[6], '#827962', 3)
trace23 = orbit(25, distance_from_sun[6], '#827962', 3)
trace24 = orbit(26, distance_from_sun[6], '#827962', 3)
trace25 = orbit(27, distance_from_sun[6], '#827962', 3)
trace26 = orbit(28, distance_from_sun[6], '#827962', 3)
layout=go.Layout(title = '太阳系', margin=dict(l=0, r=0, t=0, b=0),height=1000 , width=1500,
#paper_bgcolor = 'black',
scene = dict(xaxis=dict(title='距太阳的距离',
titlefont_color='black',
range=[-7000,7000],
backgroundcolor='black',
color='black',
gridcolor='black'),
yaxis=dict(title='距太阳的距离',
titlefont_color='black',
range=[-7000,7000],
backgroundcolor='black',
color='black',
gridcolor='black'
),
zaxis=dict(title='',
range=[-7000,7000],
backgroundcolor='black',
color='white',
gridcolor='black'
),
annotations=[
annot(distance_from_sun[0], 40, '太阳', xancr='left'),
annot(distance_from_sun[1], 5, '水星'),
annot(distance_from_sun[2], 9, '金星'),
annot(distance_from_sun[3], 9, '地球'),
annot(distance_from_sun[4], 7, '火星'),
annot(distance_from_sun[5], 30, '木星'),
annot(distance_from_sun[6], 28, '土星'),
annot(distance_from_sun[7], 20, '天王星'),
annot(distance_from_sun[8], 20, '海王星'),
]
))
fig = go.Figure(data = [trace0, trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8,
trace11, trace12, trace13, trace14, trace15, trace16, trace17, trace18,
trace21, trace22, trace23, trace24, trace25, trace26],layout = layout)
fig.show()
fig.write_html("太阳系.html")
最后效果
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved