如何在 python 中进行 3D 绘图?
我正在尝试绘制轨道轨迹。 绘制轨道轨迹
从上面的链接,我能够获得有关设置功能的帮助。但是我不知道如何在 3D 中绘图。
运行时,它不会生成正确的轨迹。
切换np.linspace
导致np.arnage
内存错误,我在运行带有 16 GB 内存的 Xubuntu 的 64 位系统上运行它。
所以我尝试转换距离单位和时间单位,但有些地方不正确。也许我的数学或其他东西。
我让149.6 * 10 ** 6 = 1 DU
。TU 是mu = DU ** 3 / TU ** 2
这样定义的,1TU = 2241.15
并且DU/TU = 66751.4
使用这些转换,我有:我也尝试使用x2,y2,z2
来查看是否可行。
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from numpy import linspace
from mpl_toolkits.mplot3d import Axes3D
mu = 1
# r0 = [-149.6 * 10 ** 6, 0.0, 0.0] # Initial position
# v0 = [29.9652, -5.04769, 0.0] # Initial velocity
u0 = [-1, 0.0, 0.0, 0.000448907, -0.0000756192, 0.0]
def deriv(u, dt):
n = -mu / np.sqrt(u[0] ** 2 + u[1] ** 2 + u[2] ** 2)
return [u[3], # dotu[0] = u[3]'
u[4], # dotu[1] = u[4]'
u[5], # dotu[2] = u[5]'
u[0] * n, # dotu[3] = u[0] * n
u[1] * n, # dotu[4] = u[1] * n
u[2] * n] # dotu[5] = u[2] * n
dt = np.arange(0.0, 20, .0001) # Time to run code in seconds'
u = odeint(deriv, u0, dt)
x, y, z, x2, y2, z2 = u.T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x2, y2, z2)
plt.show()
但这个情节也不正确。它应该是一个保持在同一轨迹上的椭圆。
#!/usr/bin/env python
# This program solves the 3 Body Problem numerically and plots the
# trajectories
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from numpy import linspace
from mpl_toolkits.mplot3d import Axes3D
mu = 132712000000.0
# r0 = [-149.6 * 10 ** 6, 0.0, 0.0] # Initial position
# v0 = [29.9652, -5.04769, 0.0] # Initial velocity
u0 = [-149.6 * 10 ** 6, 0.0, 0.0, 29.9652, -5.04769, 0.0]
def deriv(u, dt):
n = -mu / np.sqrt(u[0] ** 2 + u[1] ** 2 + u[2] ** 2)
return [u[3], # dotu[0] = u[3]'
u[4], # dotu[1] = u[4]'
u[5], # dotu[2] = u[5]'
u[0] * n, # dotu[3] = u[0] * n
u[1] * n, # dotu[4] = u[1] * n
u[2] * n] # dotu[5] = u[2] * n
dt = np.linspace(0.0, 86400 * 700, 5000) # Time to run code in seconds'
u = odeint(deriv, u0, dt)
x, y, z, x2, y2, z2 = u.T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
plt.show()