zoukankan      html  css  js  c++  java
  • python画动态图并保存视频

    1 ion()画动态图

    可以参考网页,具体示例如下:

    import numpy as np
    import matplotlib.pyplot as plt
     
    plt.axis([0, 100, 0, 1])
    plt.ion()
     
    xs = [0, 0]
    ys = [1, 1]
     
    for i in range(100):
        y = np.random.random()
        xs[0] = xs[1]
        ys[0] = ys[1]
        xs[1] = i
        ys[1] = y
        plt.plot(xs, ys) 
        plt.pause(0.1)
    

    2 保存视频

    需要安装FFMpegWriter,MACbrew install FFMpegWriter,ubuntusudo apt-get install FFMpegWriter。在ion()的基础上示例代码如下:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.animation import FFMpegWriter
    
    metadata = dict(title='Movie Test', artist='Matplotlib',comment='Movie support!')
    writer = FFMpegWriter(fps=15, metadata=metadata)
    
    fig = plt.figure()
    
    with writer.saving(fig, "writer_test.mp4", 100):  # 100指的dpi,dot per inch,表示清晰度
    
        plt.axis([0, 100, 0, 1])
        plt.ion()
        
        xs = [0, 0]
        ys = [1, 1]
        
        for i in range(100):
            y = np.random.random()
            xs[0] = xs[1]
            ys[0] = ys[1]
            xs[1] = i
            ys[1] = y
            plt.plot(xs, ys)
            plt.pause(0.1)
            writer.grab_frame()
    
  • 相关阅读:
    分苹果
    马拉车算法(求最长回文子串)
    KMP
    字典树
    关于子类和父类中的this的用法
    最长上生子序列LIS
    sass
    黑马程序员----java基础笔记下(毕向东)
    DOM
    ajax教程
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13281715.html
Copyright © 2011-2022 走看看