为什么animation.FuncAnimation的输出必须绑定到名称?

时间:2016-03-17 22:01:20

标签: python matplotlib

考虑来自matplotlib网站的动画代码taken 向右移动的正弦曲线。

    """
    A simple example of an animated plot
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation

    fig, ax = plt.subplots()

    x = np.arange(0, 2*np.pi, 0.01)
    line, = ax.plot(x, np.sin(x))


    def animate(i):
        line.set_ydata(np.sin(x + i/10.0))  # update the data
        return line,


    # Init only required for blitting to give a clean slate.
    def init():
        line.set_ydata(np.ma.array(x, mask=True))
        return line,

    ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, interval=25, blit=True)

    # Commenting out the previous line, and uncommenting the following line gives a static image. 
   #animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, interval=25, blit=True)
    plt.show()

此代码与我在Matplotlib上链接的代码相同,除了 plt.show()之前的两条注释行。请注意,在将animation.FuncAnimation()的输出绑定到名称ani之后,此名称不会在plt.show()的参数中的任何位置使用!

因此,我认为这种绑定是多余的,我删除了ani=部分。 结果是我得到了静态图像。只要animation.FuncAnimation的输出绑定到某事,动画就会按预期工作。否则不是。

这里发生了什么?

我在Ubuntu 14.04上使用Python 2.7.11(Anaconda发行版)。

0 个答案:

没有答案