你们要的Python绘画3D太阳系详细代码
作者:微小冷 时间:2021-05-12 09:32:48
用Python画一个平面的太阳系得到一些朋友的欣赏,然后有同学提出了绘制三维太阳系的要求。
从Python画图的角度来说,三维太阳系其实并不难,问题在于八大行星对黄道面的倾斜太小,所以尽管画个三维的图,但就观感而言,无非是把二维的嵌入到三维空间罢了。
来点小行星
代码如下
from os import cpu_count
import numpy as np
from numpy.random import rand
import matplotlib.pyplot as plt
from matplotlib import animation
au,G,RE,ME = 1.48e11,6.67e-11,1.48e11,5.965e24
m = np.array([3.32e5,0.055,0.815,1,0.107,317.8])*ME*G
r = np.array([0,0.387,0.723,1,1.524,5.203])*RE
v = np.array([0,47.89,35.03,29.79,24.13,13.06])*1000
theta = rand(len(m))*np.pi*2
cTheta,sTheta = np.cos(theta), np.sin(theta)
xyz = r*np.array([cTheta, sTheta, 0*r]) #位置三分量,因为参数太多,所以把这三个分量写在了一起
uvw = v*np.array([-sTheta, cTheta, 0*v]) #速度三分量
N_ast = 100
m_ast = rand(N_ast)*1e20
r_ast = (rand(N_ast)*3.5+1.6)*RE
v_ast = np.sqrt(G*3.32e5*ME/r_ast) #小行星速度sqrt(GM/R)
theta = rand(N_ast)*np.pi*2
phi = (rand(N_ast)-0.5)*0.3 #给一个随机的小倾角
cTheta,sTheta = np.cos(theta), np.sin(theta)
cPhi,sPhi = np.cos(phi),np.sin(phi)
xyza = r_ast*np.array([cTheta*cPhi, sTheta*cPhi, sPhi])
uvwa = v_ast*np.array([-sTheta*cPhi, cTheta*cPhi, sPhi])
name = "solar.gif"
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(projection='3d')
ax.grid()
ax.set_xlim3d([-5.5*RE,5.5*RE])
ax.set_ylim3d([-5.5*RE,5.5*RE])
ax.set_zlim3d([-5.5*RE,5.5*RE])
traces = [ax.plot([],[],[],'-', lw=0.5)[0] for _ in range(len(m))]
pts = [ax.plot([],[],[],marker='o')[0] for _ in range(len(m))]
pt_asts = [ax.plot([],[],[],marker='.')[0] for _ in range(N_ast)]
N = 500
dt = 3600*50
ts = np.arange(0,N*dt,dt)
xyzs,xyzas = [],[]
for _ in ts:
xyz_ij = (xyz.reshape(3,1,len(m))-xyz.reshape(3,len(m),1))
r_ij = np.sqrt(np.sum(xyz_ij**2,0))
xyza_ij = (xyz.reshape(3,1,len(m))-xyza.reshape(3,N_ast,1))
ra_ij = np.sqrt(np.sum(xyza_ij**2,0))
for j in range(len(m)):
for i in range(len(m)):
if i!=j :
uvw[:,i] += m[j]*xyz_ij[:,i,j]*dt/r_ij[i,j]**3
for i in range(N_ast):
uvwa[:,i] += m[j]*xyza_ij[:,i,j]*dt/ra_ij[i,j]**3
xyz += uvw*dt
xyza += uvwa*dt
xyzs.append(xyz.tolist())
xyzas.append(xyza.tolist())
xyzs = np.array(xyzs).transpose(2,1,0)
xyzas = np.array(xyzas).transpose(2,1,0)
def animate(n):
for i in range(len(m)):
xyz = xyzs[i]
traces[i].set_data(xyz[0,:n],xyz[1,:n])
traces[i].set_3d_properties(xyz[2,:n])
pts[i].set_data(xyz[0,n],xyz[1,n])
pts[i].set_3d_properties(xyz[2,n])
for i in range(N_ast):
pt_asts[i].set_data(xyzas[i,0,n],xyzas[i,1,n])
pt_asts[i].set_3d_properties(xyzas[i,2,n])
return traces+pts+pt_asts
ani = animation.FuncAnimation(fig, animate,
range(N), interval=10, blit=True)
plt.show()
ani.save(name)
来源:https://blog.csdn.net/m0_37816922/article/details/120736907
标签:Python,绘画,太阳系
0
投稿
猜你喜欢
AspJpeg组件:介绍、注册、高级使用方法
2010-01-25 12:42:00
Python控制浏览器自动下载歌词评论并生成词云图
2022-04-17 11:24:56
php+ajax+h5实现图片上传功能
2024-05-22 10:05:39
python实现计算资源图标crc值的方法
2022-07-02 17:57:56
python xlwt如何设置单元格的自定义背景颜色
2022-07-25 10:41:05
MySQL索引的一些常见面试题大全(2022年)
2024-01-13 00:17:30
在Python中调用ggplot的三种方法
2023-08-23 00:40:58
element弹窗表格的字体模糊bug解决
2024-04-18 10:53:25
分享Python 中的 7 种交叉验证方法
2023-09-18 19:10:33
Django中ajax发送post请求 报403错误CSRF验证失败解决方案
2021-06-11 19:47:32
理解SQL SERVER中的逻辑读,预读和物理读
2012-01-05 19:32:29
python 使用plt画图,去除图片四周的白边方法
2022-02-07 19:34:41
python读文件逐行处理的示例代码分享
2023-03-17 03:54:04
修改vue+webpack run build的路径方法
2024-04-28 10:54:08
基于spring boot 日志(logback)报错的解决方式
2022-05-12 08:13:46
PyTorch CUDA环境配置及安装的步骤(图文教程)
2022-06-19 14:00:28
python numpy数组复制使用实例解析
2023-06-22 07:27:06
Python应用之利用pyecharts画中国地图
2023-05-27 16:45:40
如何在sae中设置django,让sae的工作环境跟本地python环境一致
2022-03-09 22:04:54
Pandas数据结构中Series属性详解
2021-12-13 22:32:17