【问题标题】:Make a matplotlib animation, by clf()'ing each frame制作 matplotlib 动画,通过 clf()'ing 每一帧
【发布时间】:2014-08-06 13:15:53
【问题描述】:

我正在尝试使用animation.FuncAnimation 制作最简单的matplotlib 动画。我不在乎效率。我不想跟踪绘制的线并更新它们的数据(在我想要的应用程序中这会很烦人),我只是想在为每一帧设置动画之前擦除绘图。我认为这样的事情应该可以工作,但它不是..

import matplotlib.animation as animation

fig = Figure()

def updatefig(i):
    clf()
    p = plot(rand(100))
    draw()

anim = animation.FuncAnimation(fig, updatefig, range(10))

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    至少这似乎可行:

    import matplotlib.animation as animation
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure()
    
    def updatefig(i):
        fig.clear()
        p = plt.plot(np.random.random(100))
        plt.draw()
    
    anim = animation.FuncAnimation(fig, updatefig, 10)
    anim.save("/tmp/test.mp4", fps=1)
    

    原始代码的问题是 Figure 用大写 F 编写(应该是 figure)。

    否则,我建议不要对matplotlib 使用pylab 样式的“同一个命名空间中的所有内容”方法。另外,使用面向对象的接口代替plt.drawplt.plot等,以后会省去很多麻烦。

    【讨论】:

    • 呃!这很尴尬。但是,您的代码执行没有错误,但不会产生动画。你测试过这个吗?
    • 是的,我测试了它,它可以工作 - 但没有显示保存命令...现在已编辑为答案。
    • 谢谢。 vlc 播放结果 avi 只是一个帧,但 totem 有效。奇怪的。无论如何,我尝试使用JSAnimation 将其嵌入到 ipython 笔记本中,但我似乎没有工作....但我想这是一个单独的问题
    • 我注意到 VLC 在播放具有非标准 FPS 的视频文件时存在问题,例如1 而不是 25 或 30。所以这可能就是原因。
    猜你喜欢
    • 2022-01-12
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2022-01-22
    • 2022-12-09
    相关资源
    最近更新 更多