Exporting animated gif using matplotlib

前端 未结 1 1930
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 07:29

I\'ve been trying to export an animated gif on my windows computer for several days.

Here is the basic code:

import numpy as np
import matplotlib.pyp         


        
相关标签:
1条回答
  • 2020-12-10 07:39

    With the code from the question you are basically asking to produce an animated gif using the MPEG writer. The MPEG writer however is only able to produce videos.

    The standard way to produce an animated gif through the matplotlib animation module is to use ImageMagick.

    So first change your line to

    ani.save("test.gif",writer="imagemagick")
    

    Now for this to work the rcParam animation.convert_path must point to the ImageMagick's convert program.
    It seems you are on windows here, so it's best to include the complete path to it. Hence, before the saving, set

    plt.rcParams["animation.convert_path"] = "C:\ProgramFiles\ImageMagick\convert.exe" 
    

    or whatever your path to the convert.exe may be.

    Now it may apparently happen that convert.exe is not part of newer versions of imageMagick anymore. The documentation says

    If these tools are not available, you can simply append them to the magick tool like this:
    magick convert.

    For the matplotlib animation this means that you would need to set an extra argument. Set the path to

    plt.rcParams["animation.convert_path"] = "C:\ProgramFiles\ImageMagick\magick.exe"
    

    then call

    ani.save("test.gif",writer="imagemagick", extra_args="convert")
    
    0 讨论(0)
提交回复
热议问题