• matplotlib-动态图


    python 绘制动态图有几种方法,本文做个汇总

    plt.pause(time):动态图必须有这句,否则图像会一闪而过

    直接更新数据

    每次画图时,更新数据,实际上还是平常的画图,但数据变了,看起来像图变了;

    注意这种方法要事先指定 figure,每次画图时清空

    import matplotlib.pyplot as plt
    
    
    fig, ax = plt.subplots()    # 必须制定 fig,否则每次 自动创建 fig,图像不连续
    y1=[]
    for i in range(10):
        y1.append(i)    # 更新数据
        ax.cla()        # 清除
       plt.xlim(0, 20)
        ax.bar(y1, label='test', height=y1, width=0.3)  # 画 更新后的数据
        ax.legend()
        plt.pause(0.3)

    交互模式

    进入交互模式后,再画图,效果就是动态的

    ion:进入交互模式,直接显示图片,无需 show

    ioff:退出交互模式

    如果手动退出交互模式,需要 show 才能显示图片;

    如果不手动退出,就会自动退出,自动退出后,不管是否 show,都无法显示图片

    import matplotlib.pyplot as plt
    
    
    # plt.close()  # clf() # 清图 cla() # 清坐标轴 close() # 关窗口
    # fig = plt.figure()
    # ax = fig.add_subplot(1, 1, 1)
    
    ### 进入交换模式
    # 交互模式可直接显示图片,无需 show()
    # 交互模式可手动退出 ioff(),手动退出后需要加入 show() 才能显示图片
    # 如果不手动退出,会自动退出,自动退出后,不管是否 show(), 都无法显示图片
    plt.ion()  # interactive mode on
    
    for t in range(2):
        for s in range(20):
            plt.scatter(s, t, c='b', marker='.')  # 散点图
            # ax.lines.pop(1) 删除轨迹
            plt.pause(0.001)    # 必须有,否则一闪而过,根本看不见
            # plt.draw()
    
    ### 退出交换模式
    # plt.ioff()
    
    ### 退出交互模式后,需要显示地显示图片
    plt.show()

    FuncAnimation

    这个貌似比较强大,但比较麻烦,自己看参考资料吧,我还没用过

    参考资料:

    https://zhuanlan.zhihu.com/p/150280326  FuncAnimation

    https://www.cnblogs.com/-wenli/p/11894601.html  FuncAnimation

    https://blog.csdn.net/suzyu12345/article/details/78338091  FuncAnimation

  • 相关阅读:
    JS处理日期&字符串格式相互转换
    Ajax 原理过程 同步与异步区别 优缺点
    元素居中
    width:100%;与width:auto;的区别
    URL、URI和URN三者之间的区别
    JavaScript encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent()
    CSS 笔记三(Tables/Box Model/Outline)
    CSS 笔记二(Text/Fonts/Links/Lists)
    CSS 笔记一(Selectors/ Backgrounds/ Borders/ Margins/ Padding/ Height and Width)
    javascript,jQuery,trim()
  • 原文地址:https://www.cnblogs.com/yanshw/p/14646557.html
Copyright © 2020-2023  润新知