• animation绘制动画图


    import  numpy
    from  matplotlib  import  pyplot
    from  matplotlib  import   animation
    
    
    
    def  update_points(num):
        point_ani.set_data(x[num],y[num])  #更新点的位置,将这里的(x[num],y[num])替换(x[0],y[0])
        if num%5==0:
            point_ani.set_marker("*")
            point_ani.set_markersize(12)
        else:
            point_ani.set_marker("o")
            point_ani.set_markersize(8)
    
        text_pt.set_position((x[num],y[num])) #更新文本位置
        text_pt.set_text("x=%.3f,y=%.3f"%(x[num],y[num]))  #更新文本内容
        return point_ani,text_pt,
    
    
    
    x=numpy.linspace(0,2*numpy.pi,100)
    y=numpy.sin(x)
    
    fig=pyplot.figure(tight_layout=True)
    pyplot.plot(x,y)
    pyplot.grid(ls="--")    #给图像添加网格
    
    
    
    text_pt = pyplot.text(4, 0.8, '', fontsize=16)
    point_ani,= pyplot.plot(x[0], y[0], "ro")  #声明点对象,必须有逗号,表示得到元组
    
    #开始制作动画
    ani=animation.FuncAnimation(fig,update_points,numpy.arange(0,100),interval=100,blit=True)
    
    ani.save("test2.gif", writer='pillow',fps=10)  #保存动图
    
    
    pyplot.show()

    #上面的代码中,首先定义了一个update_points函数,用于更新绘制的图中的数据点。此函数的输入参数num代表当前动画的第几帧,
    #函数的返回,即为我们需要更新的对象,需要特别注意的是:reuturn point_ani,这个逗号一定加上,否则动画不能正常显示。当
    #然这里面操作的点对象point_ani我们一般会提前声明得到:point_ani, = plt.plot(x[0], y[0], "ro")。接下来就是将此函数传
    #入我们的FuncAnimation函数中,函数的参数说明可以参见官网,这里简要说明用到的几个参数。

    #第1个参数fig:即为我们的绘图对象.
    #第2个参数update_points:更新动画的函数.
    #第3个参数np.arrange(0, 100):动画帧数,这需要是一个可迭代的对象。
    #interval参数:动画的时间间隔。
    #blit参数:是否开启某种动画的渲染。

  • 相关阅读:
    flask 之定时任务开发
    locust 参数,数据详解
    ab返回结果参数分析
    ab使用命令
    django 实现同一个ip十分钟内只能注册一次(redis版本)
    django开发中利用 缓存文件 进行页面缓存
    django 实现登录时候输入密码错误5次锁定用户十分钟
    django 实现同一个ip十分钟内只能注册一次
    钉钉消息通知机器人python版
    用WMI监控IIS
  • 原文地址:https://www.cnblogs.com/luckiness/p/13183547.html
Copyright © 2020-2023  润新知