python模拟并不平滑,而且生成输出也需要很多时间

2024-06-01 01:28:12 发布

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

目前我正在研究两体问题,涉及行星围绕太阳的运动。为了做到这一点,为了制作动画,我使用了FuncAnimationof matplotlib.animation。在该代码中x_graphy_graph包含运动的值。要完成一次旋转,帧必须为6282。但要做到这一点,输出需要花费大量时间才能完成,动画也不平滑

fig = plt.figure()
p1 = fig.add_subplot(111)

l1, = p1.plot([],[])
l2, = p1.plot([-0.8],[0],marker= 'o')

print(func(0.98, -np.pi, 0, 0, 0.001))

plt.xlim(-1.2,1.5)
plt.ylim(-1.5,1.5)
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Planet\'s orbit')

def polar_animator(i):
    l1.set_data(x_graph[:i], y_graph[:i])
    return l1,
    
ani = animation.FuncAnimation(fig, polar_animator, frames= len(x_graph), interval=1, blit=True)
ani.save('planet.mp4', writer= 'ffmpeg')

请假设您已经有了x_图和y_图的值


Tags: l1plotfig动画plt行星graphanimation