matinal:Python matplotlib

本文介绍了Matplotlib中用于制作动画的关键类Animation、FuncAnimation和ArtistAnimation,详细阐述了如何准备数据、创建动画以及保存动画的方法,重点展示了动态图的绘制步骤和注意事项。
摘要由CSDN通过智能技术生成

1. Animation 概述

Animation 是matplotlib模块制作实时动画的动画类,包含三个子类

    Animation 是动画类的基类
    TimedAnimation 是 Animation的子类,可通过绘制时间绘制每一帧动画
    FuncAnimation 是基于Timed子类,可以通过重复调用fun()方法来绘制动画
    ArtistAnimation 使用一组Artist对象来绘制动画

    绘制动画特点
        绘制对象引用:动画对象要在制作动画时要保持长期有效,否则会被系统资源回收,动画暂停
        动画计时器:是对动画对象推进的唯一引用对象
        动画保存:需要使用animation.save、animation.To_html5_video或animation.To_jshtml进行动画保存
        matpoltlib.animation 还提供关于电影格式的类
    动画制作方法

    matplotlib.animation.Animation()是动画类的基类,是不能被使用的。常用的两个类主要animation两个子类
        matplotlib.animation.FuncAnimation

            matplotlib.animation.FuncAnimation(fig, func,
            frames=None,
            init_func=None,
            fargs=None,
            save_count=None,
            * , cache_frame_data=True,
            **kwargs)
            复制代码

        matplotlib.animation.ArtistAnimation

            matplotlib.animation.ArtistAnimation(fig,
            artists,  
            *args,  
            **kwargs)
            复制代码

2. 绘制动态图步骤

matplotlib 绘制动态图最重要的是要准备好每一帧显示的数据,通常我们使用FuncAnimation可以传入产生连续数字的func方法,因此绘制动态图主要步骤为:

    导入绘制图形的matplotlib.pyplot和制作动态图的matplotlib.animation

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    复制代码

    使用Pyplot.subplots创建一个fig画布对象和一组子图

    fig,ax = plt.subplots()
    复制代码

    调用numpy.random或者numpy.arange()等方法准备x,y轴数据

    x = np.arange(0, 2*np.pi, 0.01)
    复制代码

    Axes对象调用plot()、scatter()、hist()等绘制方法,并赋值给list对象

    line, = ax.plot(x, np.cos(x),color="pink")
    复制代码

    需要定义一个专门update data方法生成每一帧显示的数据例如func()

    def update(i):
        line.set_ydata(np.cos(x + i / 50))
        return line,
    复制代码

    调用animation.FuncAnimation把fig和update()方法

    ani = animation.FuncAnimation(
        fig, update, interval=20, blit=True, save_count=50)
    复制代码

    调用plt.show()显示出动态图

    plt.show()
    复制代码

    我们可以调用animation.save("movie.gif",writer="pillow")保存动画为gif格式

ps:我们需要提前pip install pillow 安装pillow库,否则会提示无法使用

     
    ani.save("movie.gif",writer='pillow')
     
    复制代码

3. 小试牛刀

我们使用animation类绘制直方动态图,在绘制的过程中需要注意几点

    使用numpy.linspace生成100个在-5,5的等差数列
    使用numpy.random.randn()生成随机数据
    Axes对象调用hist()返回n,bins,BarContainer
    定义一个递归update()函数,使用Python闭包跟踪barcontainer来更新每次直方图矩形高度
    调用animation.FuncAnimation()方法绘制动态图

    def drawanimationhist():
        fig, ax = plt.subplots()
        BINS = np.linspace(-5, 5, 100)
        data = np.random.randn(1000)
        n, _ = np.histogram(data, BINS)
        _, _, bar_container = ax.hist(data, BINS, lw=2,
                                      ec="b", fc="pink")
        def update(bar_container):
            def animate(frame_number):
                data = np.random.randn(1000)
                n, _ = np.histogram(data, BINS)
                for count, rect in zip(n, bar_container.patches):
                    rect.set_height(count)
                return bar_container.patches
            return animate
     
        ax.set_ylim(top=55)
     
        ani = animation.FuncAnimation(fig, update(bar_container), 50,
                                      repeat=False, blit=True)
        plt.show()
    复制代码

总结

本期,我们对matplotlib模块制作动态图类animation相关方法学习。在绘制动态图过程中,需要定义func方法来更新每一帧所需要的数据

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值