2013-06-20 127 views
12

我发现在动画这个奇妙的简短的教程:matplotlib imshow():如何动画?

http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

但是我不能产生动画imshow同样的方式()的情节。 我试图取代一些线路:

# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0, 10), ylim=(0, 10)) 
#line, = ax.plot([], [], lw=2) 
a=np.random.random((5,5)) 
im=plt.imshow(a,interpolation='none') 
# initialization function: plot the background of each frame 
def init(): 
    im.set_data(np.random.random((5,5))) 
    return im 

# animation function. This is called sequentially 
def animate(i): 
    a=im.get_array() 
    a=a*np.exp(-0.001*i) # exponential decay of the values 
    im.set_array(a) 
    return im 

,但我遇到错误 你能帮助我得到这个运行? 预先感谢您。 最好,

+1

作为一个方面说明,这是很好的做法,包括你在你的问题得到什么错误。 – tacaswell

回答

12

你很近,但是有一个错误 - initanimate应该返回iterables含正在动画的艺术家。这就是为什么在Jake的版本中,它们返回line,(实际上是一个元组),而不是line(这是一个单独的行对象)。可悲的是,这个文件不清楚!

您可以修复你的版本是这样的:

# initialization function: plot the background of each frame 
def init(): 
    im.set_data(np.random.random((5,5))) 
    return [im] 

# animation function. This is called sequentially 
def animate(i): 
    a=im.get_array() 
    a=a*np.exp(-0.001*i) # exponential decay of the values 
    im.set_array(a) 
    return [im] 
+0

美丽! 这个逗号符号让我困惑,但这帮助我! – user1805743

+0

是的,我发现'[list]'更清晰 –