Any reason why matplotlib animations would only work in interactive session?

后端 未结 1 744
暗喜
暗喜 2020-12-11 13:05

If I create a file test.py with the code

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

if __name__ == \'__main         


        
相关标签:
1条回答
  • 2020-12-11 13:35

    From the animation documentation: "[..] it is critical to keep a reference to the instance object."

    So you need to keep the FuncAnimation instance alive by assigning it to a variable.

    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation
    
    if __name__ == '__main__':
        fig = plt.figure()
        title = fig.suptitle("Test _")
    
        def anim(i):
            title.set_text("Test %d" % i)
            plt.plot([0,1], [0,1])
    
        ani = FuncAnimation(fig, anim)
        plt.show()
    


    There is an ongoing discussion about whether the Animation should be stored internally or not.

    0 讨论(0)
提交回复
热议问题