2D动画转化为3D动画

时间:2015-03-10 14:36:24

标签: python animation matplotlib 3d

我想将这个轨道行星的2D动画变成3D动画,以便在3D中可视化轨迹,如果可能的话,在轨迹上添加一条轨迹:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)

ax = plt.axes(xlim=(-10, 10), ylim=(-10, 10))
patch1 = plt.Circle((1, -1), 0.1, fc='b')
patch2 = plt.Circle((2, -2), 0.1, fc='r')
patch3 = plt.Circle((3, -3), 0.1, fc='r')

def init():
    patch1.center = (0, 0)
    patch2.center = (0, 0)
    patch3.center = (0, 0)
    ax.add_patch(patch1)
    ax.add_patch(patch2)
    ax.add_patch(patch3)
    return patch1, patch2, patch3,

def animate(i):
    x, y = patch1.center
    a, b = patch2.center
    u, v = patch3.center
    x = 0 + 3 * np.sin(np.radians(i))
    y = 0 + 3 * np.cos(np.radians(i))
    a = 0 + 1.5 * np.sin(np.radians(2*i))
    b = 0 + 1.5 * np.cos(np.radians(2*i))
    u = 0 + 5 * np.sin(np.radians(i/2))
    v = 0 + 5 * np.cos(np.radians(i/2))
    patch1.center = (x, y)
    patch2.center = (a, b)
    patch3.center = (u, v)
    return patch1, patch2, patch3,

anim = animation.FuncAnimation(fig, animate, 
                               init_func=init, 
                               frames=100000, 
                               interval=20,
                               blit=True)

plt.show()

我试过了:

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim3d(-10,10); 
ax.set_ylim3d(-10, 10);
ax.set_zlim3d(-10, 10); 

再向其余代码添加第三个坐标,但不显示任何内容......

1 个答案:

答案 0 :(得分:0)

代码here显示了一个3D动画,您应该能够直接复制粘贴并直接运行。

当然,您必须更多地考虑3D中每颗行星的轨道平面......