首页 文章

为什么FuncAnimation保存的视频是情节的叠加?

提问于
浏览
1

问候,我想问一下Python的 FuncAnimation .

在完整的代码中,我试图动画条形图(用于整体插图) . 来自的动画输出

ani = FuncAnimation(fig, update, frames=Iter, init_func = init, blit=True);
plt.show(ani);

看起来很好 .

但输出视频来自

ani.save("example_new.mp4", fps = 5)

给出了与Python中显示的动画略有不同的版本 . 与动画相比,输出提供了“叠加版本”的视频 . 与动画不同:在视频中,在每个帧中,先前的绘图与当前绘图一起显示 .

这是完整的代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


fig, ax = plt.subplots()
Num = 20
p = plt.bar([0], [0], 1, color = 'b')
Iter = tuple(range(2, Num+1))
xx = list(np.linspace(0, 2, 200)); yy = list(map(lambda x : x**2,xx));

def init(): 
    ax.set_xlim(0, 2)
    ax.set_ylim(0, 4)
    return (p)

def update(frame):
    w = 2/frame;
    X = list(np.linspace(0, 2-w, frame+1));
    Y = list(map(lambda x: x**2, X));
    X = list(map(lambda x: x + w/2,X));
    C = (0, 0, frame/Num); 
    L = plt.plot(xx , yy, 'y', animated=True)[0]
    p = plt.bar(X, Y, w, color = C, animated=True)
    P = list(p[:]); P.append(L)   
    return P

ani = FuncAnimation(fig, update, frames=Iter, init_func = init, interval = 0.25, blit=True)
ani.save("examplenew.mp4", fps = 5)
plt.show(ani)

对此的任何建设性意见将不胜感激 . 谢谢 . 此致,Arief .

1 回答

  • 1

    保存动画时,不使用blitting . 您可以关闭blitting,即 blit=False ,并以与保存相同的方式查看动画 .

    发生的事情是,在每次迭代中添加一个新的图,而不删除最后一个图 . 你基本上有两个选择:

相关问题