二维动画到三维动画

2024-09-28 05:17:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把这个环绕行星的二维动画变成一个三维动画,以便在三维中可视化轨迹,如果可能的话,给轨迹添加一个轨迹:

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()

我试过了:

^{pr2}$

再加上在代码的其余部分添加第三个坐标,但没有显示任何内容。。。在


Tags: importaddinit轨迹npfigpltax
1条回答
网友
1楼 · 发布于 2024-09-28 05:17:17

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

当然,你得多想一想3D中每个行星的轨道平面。。。在

相关问题 更多 >

    热门问题