2013-06-20 78 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]'更清晰 –