Why I got a static plot after increasing 'holding time' in matplotlib FuncAnimation?

Viewed 22

I have a dataframe including one column for image index, the other for holding time. I'd like to make a video according to it.

When all values of holding times are below 100, the video works well:

df = pd.DataFrame({'pos':[1,2,4,3], 'time':[11,10,1,10]})

But after I changed one of the time into 100, i got a static plot lasted till the end of the video. Here is the code of the wrong output (all I did was change the first value of time into 110):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as img
import matplotlib.animation as anim
from IPython.display import HTML

df = pd.DataFrame({'pos':[1,2,4,3], 'time':[110,10,1,1]})
file = 'C:\\Users\\...'    
images = [img.imread(file+f'\\{i}.jfif') for i in range(1,5)]    # four images

fig = plt.figure()
def update(frame):
    if frame is None: return  
    
    fig.clear()
    plt.imshow(images[frame-1], animated=True)
    plt.tick_params(bottom=False, left=False, labelleft=False, labelbottom=False)


def frame_generator():
    for frame, cont in zip(df.pos, df.time):
        yield frame

        if cont > 1:
            for _ in range(int(cont)-1):
                yield None
                

ani = anim.FuncAnimation(fig, update, frames=frame_generator(), interval=100 
                         ,blit=False, repeat=False)
plt.tight_layout()
HTML(ani.to_html5_video())

Thanks in advance!

0 Answers
Related