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()我正在尝试创建一个功能动画与3 jpg图像在列表中。在程序运行3张图片1次后,它给了我一个错误。当我打印t‘时,它永远不会重置为0。
错误:
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有人知道问题出在哪里吗?
发布于 2020-07-19 04:09:46
解决了。我必须添加frames = len(list)作为FuncAnimation的参数
发布于 2020-07-19 03:33:40
您未能提供到预期的MRE,并且没有完成预期的初始调试工作。因此,我不能确定。
然而,我最大的怀疑是来自update_plot的返回,它使用的是一个没有向我们展示的参数--并使用它作为对一个全局序列的下标,该序列隐藏了预定义的类型。
尝试使用以下简单技术进行调试:
def update_plot(t):
print("ENTER update_plot; t =", t, "\n list =", list)
print(t)
return list[t]我希望,就在你的失败点之前,你会看到那个t >= len(list)。
一般提示:不要给变量与内置或预定义的名称相同的名称。特别是,更改list。
https://stackoverflow.com/questions/62975763
复制相似问题