美文网首页
matplotlib动画

matplotlib动画

作者: 多问Why | 来源:发表于2019-01-25 17:34 被阅读0次

    例1.实现移动的正弦曲线

    from matplotlib import pyplot as plt
    from matplotlib import animation
    import numpy as np
    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))
        return line,
    
    def init():
        line.set_ydata(np.sin(x))
        return line,
    
    ani = animation.FuncAnimation(fig=fig,
                                  func=animate,
                                  frames=None,
                                  init_func=init,
                                  interval=1,
                                  blit=False)
    plt.show()
    

    注意ani虽然用不到,但不知道为什么不赋值不会实现动画效果。

    例2.闪烁的散点图

    from matplotlib import pyplot as plt
    from matplotlib import animation
    import numpy as np
    fig, ax = plt.subplots()
    x = np.arange(0, 2*np.pi, 0.5)
    line = ax.scatter(x, np.sin(x), c='black')
    
    
    def animate(i):
        if i%2:
            color = 'black'
        else:
            color = 'white'
        next_line = ax.scatter(x, np.sin(x), c=color)
        return next_line
    
    
    def init():
        return line
    
    
    ani = animation.FuncAnimation(fig=fig,
                                  func=animate,
                                  frames=None,
                                  init_func=init,
                                  interval=1000,
                                  blit=False)
    def save_as_gif():
        import os
        writer = animation.FFMpegWriter()
        ani.save('line.mp4',writer = writer)
    
        os.system("ffmpeg -i line.mp4 line.gif")
    save_as_gif()
    plt.show()
    

    每隔1000毫秒,颜色在白与黑间变换一次,实现闪烁效果。
    要保存图片需要安装ffmpeg并且配置到环境变量中。


    line.gif

    例3.多个子图实现动画效果

    from matplotlib import pyplot as plt
    from matplotlib import animation
    import numpy as np
    
    fig = plt.figure()
    ani_list = []
    def paint(index):
        ax = fig.add_subplot(3,1,index)
        x = np.arange(0, 2*np.pi, 0.5)
        line = ax.scatter(x, np.sin(x), c='black')
    
        def animate(i):
            if (i+index) % 2:
                color = 'black'
            else:
                color = 'white'
            next_line = ax.scatter(x, np.sin(x), c=color)
            return next_line
    
        def init():
            return line
    
        ani = animation.FuncAnimation(fig=fig,
                                      func=animate,
                                      frames=None,
                                      init_func=init,
                                      interval=1000,
                                      blit=False)
        ani_list.append(ani)#no animation if this line removed
    
    
    for i in range(3):
        paint(i+1)
    
    
    plt.show()
    

    相关文章

      网友评论

          本文标题:matplotlib动画

          本文链接:https://www.haomeiwen.com/subject/cwrvjqtx.html