2016-08-14 6 views
0

Ich habe versucht, eine Animation mit Matplolibs FuncAnimation zu einem Modell wie das Bild unten, wo jedes Bild einen Kreis ändert Farbe von weiß nach grau und umgekehrt zu erstellen . Das Bild:TypeError: 'NoneType' Objekt ist nicht iterierbaren Fehler in Matoplotlib Animation

The model

Unten ist der Code. Betrachte h als eine Liste mit separaten Listen, die erste enthält den Kreis, der in jedem Frame geändert werden soll und der zweite die Anfangsfarben der Kreise in Form von separaten "k" - und "w" -Strings. Ein Beispiel für h:

[[1,3,4,1.....]['k','w','k','w','w'....]] 

der Code:

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

die Imports verwenden, sind wie folgt (einige sind nicht für diesen Teil des Programms gemeint):

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 

Wenn ich jedoch den Code ausführe, erscheint nur der erste Frame (etwas Ähnliches wie oben beschrieben) und wenn ich das Fenster schließe erscheint dieser Fehler:

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 

Ich kann diesen Fehler nicht lösen. Vielen Dank im Voraus für jede Hilfe :).

Antwort

0

Die documentation of FuncAnimation sagt, dass die update Funktion die geänderten Künstler zurückkehren sollte:

If blit=True, func and init_func should return an iterable of drawables to clear. 

Sie können Ihre Funktion

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 

Hinweis ändern möchten die letzte Zeile, die ich zu Ihrem Code hinzugefügt.

+0

Danke. Jetzt fühle ich mich schrecklich dumm, weil ich eine Rückkehr verpasst habe. – nyenyu

+0

Nun, es ist nicht klar, dass die Funktion überhaupt etwas zurückgeben muss. –

Verwandte Themen