0

I found this question/answer with the following code How do I get a fill_between shape in Funcanimation?

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()

when I run this in spyder it works fine. however as soon as I save it (ffmpeg)

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

the fill appears from frame 0 and is refilled on top as I want.

the following images are done with a different step function but represents the same problem as with the sin(X*3) function from the example

play after running with spyder

1

play after being saved as mp4

2

How can I avoid the first fill?

I tried defining p = ax.fill_between([], [], 0, facecolor = 'C0', alpha = 0.0) in Init() but this didnt make any difference

and I also saw in a post some proposed the use .remove() or clear(). But I got either local variable error or a complete white animation respectively.

vimuth
  • 5,064
  • 33
  • 79
  • 116
felipebch
  • 1
  • 2

0 Answers0