曲线 FuncAnimation 下的动画 fill_between - 动画已经从 fill_between 开始,我该如何删除它

问题描述 投票:0回答:0

如果以下代码我发现了这个问题 如何在 Funcanimation 中获得 fill_between 形状?

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

X = np.linspace(0,3.428, num=250) 
Y = np.sin(X*3)

fig = plt.figure(figsize=(13,5), dpi=80)
ax = plt.axes(xlim=(0, 3.428), ylim=(-1,1))
line, = ax.plot([], [], lw=5)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = X[0:(i-1)]
    y = Y[0:(i-1)]
    line.set_data(x,y)
    p = plt.fill_between(x, y, 0, facecolor = 'C0', alpha = 0.2)
    return line, p,

anim = animation.FuncAnimation(fig,animate, init_func=init, 
                           frames = 250, interval=20, blit=True)
plt.show()

当我在 spyder 中运行它时,它工作正常。然而,一旦我保存它 (ffmpeg)

play after running with spyder

anim.save('test_fill.mp4', fps=30)

填充从第 0 帧开始出现,并根据需要在顶部重新填充。

play after being saved as mp4

如何避免第一次填充?

我尝试在 Init() 中定义 p = ax.fill_between([], [], 0, facecolor = 'C0', alpha = 0.0) 但这没有任何区别

我还在帖子中看到有人提议使用 .remove() 或 clear()。但是我分别得到了局部变量错误或完整的白色动画。

matplotlib-animation
© www.soinside.com 2019 - 2024. All rights reserved.