Home > Blockchain >  matplolib animation.save dont't produce a video
matplolib animation.save dont't produce a video

Time:12-18

I'm trying to save the animation produces by :

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)

But when I execute :

ani.save('animation.mp4')

Or :

ani.save('animation.gif',writer='pillow')

The code run a very long time and finally, only the last image is displayed. A mp4 or gif video is returned but, it is the same image during the entire video (not the first or the last image !).

The animation, without save, works.

CodePudding user response:

I have the solution.

It is because I this codes in the shell :

ani=fluid.animation('omega',fps=10)

The I close the window and I run :

ani.save('vortex2.mp4')

But if we close the animation, the "ani" object is not valid. We have to do :

ani=fluid.animation('omega',fps=10)
ani.save('vortex2.mp4')
  • Related