zoukankan      html  css  js  c++  java
  • Matplotlib-动画

    Animation 动画

    定义方程
    参数设置
    # Animation 动画
    
    # 定义方程
    
    # 使用matplotlib做动画也是可以的,我们使用其中一种方式,function animation来说说, 具体可参考matplotlib animation api。首先,我们做一些准备工作:
    from matplotlib import pyplot as plt
    from matplotlib import animation
    import numpy as np
    fig, ax = plt.subplots()
    # 我们的数据是一个0~2π内的正弦曲线:
    
    x = np.arange(0, 2*np.pi, 0.01)
    line, = ax.plot(x, np.sin(x))
    
    # 接着,构造自定义动画函数animate,用来更新每一帧上各个x对应的y坐标值,参数表示第i帧:
    def animate(i):
        line.set_ydata(np.sin(x + i/10.0))
        return line,
    # 然后,构造开始帧函数init:
    
    def init():
        line.set_ydata(np.sin(x))
        return line,
    # 参数设置
    
    # 接下来,我们调用FuncAnimation函数生成动画。参数说明:
    # 1fig 进行动画绘制的figure
    # 2func 自定义动画函数,即传入刚定义的函数animate
    # 3frames 动画长度,一次循环包含的帧数
    # 4init_func 自定义开始帧,即传入刚定义的函数init
    # 5interval 更新频率,以ms计
    # 6blit 选择更新所有点,还是仅更新产生变化的点。应选择True,但mac用户请选择False,否则#无法显示动画
    ani = animation.FuncAnimation(fig=fig,
                                  func=animate,
                                  frames=100,
                                  init_func=init,
                                  interval=20,
                                  blit=False)
    
    
    plt.show()
    
    #当然,你也可以将动画以mp4格式保存下来,但首先要保证你已经安装了ffmpeg 或者mencoder, 更多信息参考matplotlib animation api:
    
    # ani.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

     可以在python中直接显示动态图片

    新版Pycharm中Matplotlib图像不在弹出独立的显示窗口

    在命令行打开python然后输入代码,得到动态的函数曲线

  • 相关阅读:
    ubuntu开启SSH服务
    Ubuntu修改虚拟内存(即swap空间)
    【转】Ubuntu 13.10中MyEclipse 10.6+下载+安装+破解
    【转】 ubuntu下安装mysql
    【转】 Ubuntu 11.04 下安装配置 JDK 7
    Linux非root用户安装jdk和tomcat
    algorithm之改变序列算法--待解决
    时间日期设置--ctime头文件
    C中的一些函数
    algorithm之不变序列操作
  • 原文地址:https://www.cnblogs.com/foremostxl/p/10356959.html
Copyright © 2011-2022 走看看