Can you please help me figuring out what the problem is here? I don't know what is going wrong. Single plots from img
can be plotted just fine, but the animation module gives an error. The Traceback says:
Traceback (most recent call last):
File "/home/ckropla/workspace/TAMM/Sandkasten.py", line 33, in <module>
ani = animation.ArtistAnimation(fig, img, interval=20, blit=True,repeat_delay=0)
File "/home/ckropla/.pythonbrew/pythons/Python-3.3.1/lib/python3.3/site-packages/matplotlib/animation.py", line 818, in __init__
TimedAnimation.__init__(self, fig, *args, **kwargs)
File "/home/ckropla/.pythonbrew/pythons/Python-3.3.1/lib/python3.3/site-packages/matplotlib/animation.py", line 762, in __init__
*args, **kwargs)
File "/home/ckropla/.pythonbrew/pythons/Python-3.3.1/lib/python3.3/site-packages/matplotlib/animation.py", line 481, in __init__
self._init_draw()
File "/home/ckropla/.pythonbrew/pythons/Python-3.3.1/lib/python3.3/site-packages/matplotlib/animation.py", line 824, in _init_draw
for artist in f:
TypeError: 'AxesImage' object is not iterable
Code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def FUNCTION(p,r,t):
k_0,dx,c = p
x,y = r
z = np.exp(1j*(k_0[0]*np.meshgrid(x,y)[0]+k_0[1]*np.meshgrid(x,y)[1]-c*t))*np.exp(-((np.sqrt(np.meshgrid(x,y)[0]**2+np.meshgrid(x,y)[1]**2)-c*t)/(2*dx))**2 )*(2/np.pi/dx**2)**(1/4)
z = abs(z)
#k,F = FFT((x-c*t),y)
return(x,y,z)
#Parameter
N = 500
n = 20
x = np.linspace(-10,10,N)
y = np.linspace(-10,10,N)
t = np.linspace(0,30,n)
r=[x,y]
k_0 = [1,1]
dx = 1
c = 1
p = [k_0,dx,c]
fig = plt.figure("Moving Wavepackage")
Z = []
img = []
for i in range(n):
Z.append(FUNCTION(p,r,t[i])[2])
img.append(plt.imshow(Z[i]))
ani = animation.ArtistAnimation(fig, img, interval=20, blit=True,repeat_delay=0)
plt.show()
Each element in img
needs to be a sequence of artists, not a single artist. If you change img.append(plt.imshow(Z[i]))
to img.append([plt.imshow(Z[i])])
then your code works fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With