mjasian
mjasian

Reputation: 11

Why isn't FuncAnimation repeating when I'm put repeat = True as a parameter?

import matplotlib.image as mpimg
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Button
from matplotlib.widgets import Slider
fig = plt.figure()
image_list =  ['downloads/20120831_194836_aia.lev1_euv_12s_4k.jpg', 'downloads/20120831_194936_aia.lev1_euv_12s_4k.jpg', 'downloads/20120831_195036_aia.lev1_euv_12s_4k.jpg']

list = []


for raw_image in image_list:
    image1 = mpimg.imread(raw_image)
    real_image1 = plt.imshow(image1)
    list.append([real_image1])


def update_plot(t):
    print(t)
    return list[t]


anim = animation.FuncAnimation(fig, update_plot, repeat = True, interval=1, blit=False,
                                repeat_delay=200)
plt.show()

I am trying to create a func animation with the 3 jpg images in the list. After the program runs the 3 images 1 time, it gives me an error. When I print 't', it never resets to 0.

Error:

Traceback (most recent call last):
  File "/Users/jamisenma/opt/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 1194, in _on_timer
    ret = func(*args, **kwargs)
  File "/Users/jamisenma/opt/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1447, in _step
    still_going = Animation._step(self, *args)
  File "/Users/jamisenma/opt/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1173, in _step
    self._draw_next_frame(framedata, self._blit)
  File "/Users/jamisenma/opt/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1192, in _draw_next_frame
    self._draw_frame(framedata)
  File "/Users/jamisenma/opt/anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 1755, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "/Users/jamisenma/Library/Application Support/JetBrains/PyCharmCE2020.1/scratches/scratch_59.py", line 19, in update_plot
    return list[t]
IndexError: list index out of range

Does anyone know what the issue is?

Upvotes: 0

Views: 876

Answers (2)

mjasian
mjasian

Reputation: 11

Solved it. I had to add frames = len(list) as a parameter of FuncAnimation

Upvotes: 1

Prune
Prune

Reputation: 77857

You failed to provide into the expected MRE, and didn't do the expected initial debugging work. Therefore, I can't be sure.

However, my greatest suspicion is at the return from update_plot, which is using an argument you failed to show us -- and using that as subscript into a global sequence that shadows a pre-defined type.

Try debugging with this simple technique:

def update_plot(t):
    print("ENTER update_plot; t =", t, "\n list =", list)
    print(t)
    return list[t]

I expect that, just before your point of failure, you will see that t >= len(list).

General hint: do not give a variable the same name as a built-in or predefined name. In particular, change list.

Upvotes: 0

Related Questions