2017-05-30 97 views
0

目前我正在處理一個要分析的表面,4000個40x40幀。從機制上講,它是一個4000幀的列表,它是40行的列表,它是40個數據點的列表。Matplotlib.animation.FuncAnimation一次迭代列表

我正在使用funcanimation來查看它隨時間的變化,但我遇到了一個問題。基本上在這一點上,我有兩個問題:

1)我怎樣才能遍歷幀一次,最終被用於保存它?

2)爲什麼我現在的代碼給我一個旋轉的沙灘球,如果我試圖退出動畫?

def update_fig(*args): 
    global currentNum, numSurfaces 
    currentNum += 1 
    if currentNum < numSurfaces: 
    print(currentNum) 
    im.set_array(testClass.surfaceList[currentNum]) 
    return im, 
    else: 
     pass 


def updatefig(*args): 
    global currentNum, numSurfaces 
    currentNum += 1 
    if currentNum > numSurfaces - 1: 
    currentNum = 0 
    print currentNum 
    im.set_array(testClass.surfaceList[currentNum]) 
    return im, 

ani = animation.FuncAnimation(fig, updatefig, frames=4000, interval=0.5, repeat=False, blit=False) 
plt.show() 
plt.close() 
+0

忽略第二個問題有兩個原因:1.這兩個問題是無關的。 Stackoverflow是關於提出具體問題。如果您有兩個問題,請提出兩個問題。 2.尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建[mcve]。 – ImportanceOfBeingErnest

回答

0

在開始動畫之前,您可以通過循環幀來保存圖形。

def updatefig(i): 
    im.set_array(testClass.surfaceList[i]) 

for i in range(4000): 
    updatefig(i) 
    plt.savefig("frame{}.png".format(i)) 

ani = animation.FuncAnimation(fig, updatefig, frames=4000, interval=0.5, repeat=False) 
plt.show() 

您還可以使用動畫來保存數據

def updatefig(i): 
    im.set_array(testClass.surfaceList[i]) 

ani = animation.FuncAnimation(fig, updatefig, frames=4000, interval=0.5, repeat=False) 
ani.save("frames.png", writer="imagemagick") 
plt.show() 

這第二種方法僅適用於PNG圖像,需要ImageMagick的安裝。