【问题标题】:Matplotlib Animate does not render when blit is set to be True当 blit 设置为 True 时,Matplotlib Animate 不渲染
【发布时间】:2021-04-14 21:02:02
【问题描述】:

我正在尝试在函数中使用 animation.FuncAnimation。目标是在两体模型中对轨道进行动画演示。

调用动画的函数设置如下:

def plot_animate(r, bod_rad, steps, dt):
    #Setup plot environment
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection='3d')
    max_val = np.max(np.abs(r))
    ax.set_xlim([-max_val, max_val])
    ax.set_ylim([-max_val, max_val])
    ax.set_zlim([-max_val, max_val])

    #Plot background and body to orbit
    ax = plot_bckground(ax, bod_rad)
    #Setup initial position and current position
    ax.plot([r[0,0]],[r[0, 1]], [r[0,2]],'ko', label='Starting Position', zorder=20)
    orb, = ax.plot(r[0,0], r[0,1], r[0,2], 'k--', label='trajectory', zorder=10)
    pos, = ax.plot([r[0,0]],[r[0, 1]], [r[0,2]],'go', label='Current Position', zorder=10)

    #Animate trajectory
    anime = animation.FuncAnimation(fig,  orbit_anim, fargs=(ax, r, orb, pos),\
                                    frames=steps, interval=dt, blit=True)
    
    plt.legend()
    plt.show()

plt_background 添加球体和起点的图。 orbit_anim 如下:

def orbit_anim(frame, ax, r, orb, pos):
    #Trajectory and current position implementation to animate the satellite
    orb = ax.plot(r[:frame+1, 0], r[:frame+1, 1], r[:frame+1, 2], 'k--', label='trajectory', zorder=10)
    pos.set_data(r[frame, 0], r[frame, 1])
    pos.set_3d_properties(r[frame, 2], 'z')
    return orb

当 blit 为 false 时,代码按预期工作。绿色的“当前位置”点引导轨道轨迹线并被渲染。 但是,当 blit 设置为 true 时,代码仍然有效,但不再自动呈现绿色的“当前位置”。它仅在我更改 3d 绘图视图的视角且不再引导轨迹时显示。

【问题讨论】:

    标签: matplotlib-animation


    【解决方案1】:

    这个错误是因为我没有更新值,而是再次渲染了整行,我猜这会覆盖pos艺术家对象的渲染。当我改为更改数据时,渲染工作:

    def orbit_anim(frame, ax, r, orb, pos):
        orb.set_data(r[:frame+1, 0], r[:frame+1, 1])
        orb.set_3d_properties(r[:frame+1, 2], 'z')    
        pos.set_data(r[frame, 0], r[frame, 1])
        pos.set_3d_properties(r[frame, 2], 'z')
        return orb, pos
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 2013-05-20
      相关资源
      最近更新 更多