【问题标题】:matplotlib animation save is not obeying blit=True but it seems to work just fine in plt.show()matplotlib 动画保存不遵守 blit=True 但它似乎在 plt.show() 中工作得很好
【发布时间】:2019-06-20 12:29:48
【问题描述】:

我对 Python 很陌生,正在尝试使用 matplotlib 为文本设置动画。使用了几个在线示例得出以下代码:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

plt.xlabel('Distance')
plt.ylabel('Height')
plt.title('Object Trajectory \n')

plt.legend(loc="upper right", markerscale=4, fontsize=10)
plt.grid()

text=ax.text(3,1,'Moving Text', ha="left", va="bottom",clip_on=True,rotation=90,fontsize=15)    
text2=ax.text(0,1,'Moving Text', ha="left", va="bottom",clip_on=True,fontsize=15)    

def init():
    ax.set_xlim(0,10)
    ax.set_ylim(0,10)
    return text,text2

def update(frame):        
    #Moving a text
    text=ax.text(3,1+(int(frame))/30,'Moving Text', ha="left", va="bottom",clip_on=True,rotation=90,fontsize=15)    
    text2=ax.text(0+(int(frame))/30,1,'Moving Text', ha="left", va="bottom",clip_on=True,fontsize=15)    

    return text,text2

anim = animation.FuncAnimation(fig, update, init_func=init, frames=120, interval=10, blit=True)

anim.save('try_animation.mp4',dpi=160,fps=30, writer="ffmpeg")

plt.show()

所以当我在控制台中运行它时,我可以看到文本很好地移动。但是当我将它保存到 MP4 文件时,文本似乎并没有出现 blit。请帮忙。

谢谢

This is a screenshot of saved video file

【问题讨论】:

    标签: python matplotlib animation save blit


    【解决方案1】:

    您观察到的是预期的行为。 Blitting 是一种用于仅刷新图形输出的一部分的技术。在 matplotlib 的情况下,不是绘制完整的图形,而是只刷新它的一部分,即轴内的区域,并且只绘制动画函数返回的那些艺术家。这允许在屏幕上具有更快的动画速度。

    但是,在保存动画时,需要完整绘制每一帧。

    因此,为了让文本移动,应该更新单个文本的位置,而不是一遍又一遍地创建新文本。这可以通过

    text.set_position((x,y))
    

    该示例因此看起来像

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    fig, ax = plt.subplots()
    
    plt.xlabel('Distance')
    plt.ylabel('Height')
    plt.title('Object Trajectory \n')
    plt.grid()
    
    text=ax.text(3,1,'Moving Text', ha="left", va="bottom",clip_on=True,rotation=90,fontsize=15)    
    text2=ax.text(0,1,'Moving Text', ha="left", va="bottom",clip_on=True,fontsize=15)    
    
    def init():
        ax.set_xlim(0,10)
        ax.set_ylim(0,10)
        return text,text2
    
    def update(frame):        
        #Moving a text
        text.set_position((3, 1+(int(frame))/30))
        text2.set_position((0+(int(frame))/30,1))
        return text,text2
    
    anim = animation.FuncAnimation(fig, update, init_func=init, frames=120, interval=10, blit=True)
    
    anim.save('try_animation.mp4',dpi=160,fps=30, writer="ffmpeg")
    
    plt.show()
    

    【讨论】:

    • 非常感谢。我有一个更大的代码,我也在其中尝试移动多边形。但为了缩短问题细节,我删除了那部分。我也会尝试对原始代码进行更正。
    • 如何对 matplotlib 图上的常规点(或坐标)执行此操作? ax.plot(x,y) 保存为 gif 文件等时似乎没有 blit。
    【解决方案2】:

    对于line2D 对象,您可以对移动点使用set_data( ) 方法:

    line , = ax.plot( x[0], y[0] , "ro")
    
    ...
    
    def animar(i):
       line.set_data( x[i] , y[i], "ro" )
       return line
    

    matplotlib.animation 示例中,有一个采用这种方法的小脚本。

    【讨论】:

    • 欢迎来到 StackOverflow!目前尚不清楚您究竟是如何尝试调用动画保存,以及 blit=True 参数出现在哪里。请编辑您的问题,以便其他用户回答。
    猜你喜欢
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多