4
我使用matplotlib的FuncAnimation功能動畫大數據集的片段:如何重置matplotlib動畫,而無需重新運行腳本
fig = plt.figure(figsize=(15, 11.5))
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False,
xlim=(x1,x2), ylim=(y1, y2))
data,=ax.plot([],[],'o')
def init():
data.set_data([],[])
return data
def animate(t):
global x_pos,y_pos
data.set_data(x_pos[t],y_pos[t])
return data
ani=animation.FuncAnimation(fig,animate,frames=duration,interval=20,
init_func=init,blit=True)
plt.show()
當我從頭開始我的代碼運行這一偉大工程。但是,由於需要加載並預處理大型數據集,因此需要幾分鐘的時間,所以我希望能夠運行我的代碼的一部分來測試和生成動畫。當我關閉我的動畫並嘗試再次運行它時,我只留下一個空白圖 - 沒有繪製點並且animate()函數從不被調用(我用print語句測試了這個)。
我試圖清除情節和圖:
plt.clf()
fig.close()
plt.clear(figure)
,並嘗試不同的圖號,但結果是一樣的。
如何清除動畫,以便我可以在不重新運行整個腳本的情況下再次運行它?