我正在嘗試保存由以下內容生成的影片:
def aff(self,frame,attr,vmin=None,vmax=None,born=True):
val = getattr(self,attr)
if born:
plt.imshow(val[frame],extent=(0,self.L,self.L,0),cmap="seismic",vmin=vmin,vmax=vmax)
else:
plt.imshow(val[frame],extent=(0,self.L,self.L,0),cmap="seismic")
plt.colorbar()
def animation(self,attr,fps=5,born=True,begin=0):
def func(frame):
plt.clf()
self.aff(frame,attr,vmin,vmax,born)
val = getattr(self,attr)
if born:
vmin = np.min(val)
vmax = np.max(val)
fig = plt.figure()
return anim.FuncAnimation(fig,func,np.arange(begin,val.shape[0]),interval=1/fps*1000)
但是當我執行:
ani.save('animation.mp4')
或者 :
ani.save('animation.gif',writer='pillow')
代碼運行了很長時間,最后只顯示最后一張圖片。回傳一個 mp4 或 gif 視頻,但在整個視頻中它是相同的影像(不是第一個或最后一個影像!)。
影片無需保存即可作業。
uj5u.com熱心網友回復:
我有解決方案。
這是因為我在 shell 中撰寫了這段代碼:
ani=fluid.animation('omega',fps=10)
我關閉視窗并運行:
ani.save('vortex2.mp4')
但是如果我們關閉影片,“ani”物件就無效了。我們必須做 :
ani=fluid.animation('omega',fps=10)
ani.save('vortex2.mp4')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383833.html
標籤:Python matplotlib 动画片
