我一直在尝试使用matplolibs FuncAnimation对动画进行编码,以创建如下图所示的模型,其中每个框架的圆圈颜色从白色变为灰色,反之亦然。图片:
以下是代码。将h视为包含单独列表的列表,第一个包含要在每个帧中更改的圆,第二个包含以单独的' k'形式的圆的起始颜色。和' w'字符串。 h的例子:
[[1,3,4,1.....]['k','w','k','w','w'....]]
代码:
n=5
fig = plt.figure(figsize=(n,n))
x =[i%n+1 for i in range(0,n**2)]
y =[i/n+1 for i in range(n**2)]
h=giving_data_for_visual(2,n,3,1,2)
cl=h[1]
colors=''
for i in cl:
colors+=i
changes_list=h[0]
scat=plt.scatter(x, y,s=50,facecolors=colors, alpha=0.5)
def update(frame):
global colors,change_list
t=(cl[changes_list[frame]]=='k')
cl[changes_list[frame]]='k' if t else 'w'
colors=cl
scat.set_facecolors(colors)
ani=animation.FuncAnimation(fig, update, interval=10, blit=True,frames=len(changes_list))
plt.show()
导入使用的内容如下(有些不适用于此部分程序):
from random import randint
from random import uniform
from math import exp
from math import log
import matplotlib.pyplot as plt
import numpy as np
from numpy import std
from matplotlib import animation
然而,当我运行代码时,只显示第一帧(与上面发布的模型类似),当我关闭其窗口时出现此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\lib-tk\Tkinter.py", line 1536, in __call__
return self.func(*args)
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\lib-tk\Tkinter.py", line 587, in callit
func(*args)
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 143, in _on_timer
TimerBase._on_timer(self)
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\matplotlib\backend_bases.py", line 1290, in _on_timer
ret = func(*args, **kwargs)
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\matplotlib\animation.py", line 925, in _step
still_going = Animation._step(self, *args)
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\matplotlib\animation.py", line 784, in _step
self._draw_next_frame(framedata, self._blit)
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\matplotlib\animation.py", line 802, in _draw_next_frame
self._pre_draw(framedata, blit)
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\matplotlib\animation.py", line 815, in _pre_draw
self._blit_clear(self._drawn_artists, self._blit_cache)
File "C:\Users\Yael\Desktop\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\matplotlib\animation.py", line 853, in _blit_clear
axes = set(a.axes for a in artists)
TypeError: 'NoneType' object is not iterable
我无法解决这个错误。在此先感谢您的帮助:)。
答案 0 :(得分:0)
documentation of FuncAnimation
表示update
函数应返回已更改的艺术家:
If blit=True, func and init_func should return an iterable of drawables to clear.
您可能希望将功能更改为
def update(frame):
global colors,change_list
t=(cl[changes_list[frame]]=='k')
cl[changes_list[frame]]='k' if t else 'w'
colors=cl
scat.set_facecolors(colors)
return scat
请注意我添加到代码中的最后一行。