Python绘图在3d中

时间:2013-04-17 01:56:00

标签: python matplotlib

如何在python中用3D绘图?

我正在尝试绘制轨道轨迹。 Plotting Orbital Trajectories

通过上面的链接,我可以获得设置功能的帮助。但是我不知道如何用3D绘图。

运行此操作时,它不会生成正确的轨迹。

np.linspace切换到np.arnage会导致内存错误,我在运行Xubuntu的64位系统上使用16 GB的Ram运行。

所以我尝试转换距离单位和时间单位,但有些不正确。也许是我的数学或其他。

我让149.6 * 10 ** 6 = 1 DU。 TU定义为mu = DU ** 3 / TU ** 2所以1TU = 2241.15DU/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()

但这个情节也不正确。它应该是一个保持在相同轨迹上的椭圆。

enter image description here

#!/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()

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以从该页面的@sashkello中获取前几行,并插入您从颂歌解算器获得的xyz

http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html复制:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#<<solve for x, y, z here>>#

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
plt.show()