zoukankan      html  css  js  c++  java
  • matplotlib 显示最后n条数据(可用于实时更新)

    2020-04-16 14:05:01 --Edit by yangray

    按横轴刻度的种类不同,分为数值类刻度和日期类刻度。

    • 数值类刻度

        需求:x轴数据间隔为2,显示最后24条数据。

    #!/usr/bin/python
    # _*_ Coding: Utf-8 _*_
    
    import random
    import matplotlib.pyplot as plt
    
    xs = [i*2 for i in range(200)]
    ys = [10 + random.randint(1, 8) for i in range(len(xs))]
    
    # 绘制
    plt.plot(xs, ys)
    
    '''
        仅显示最后n个数据
        确定一个限位框(由左下角和右上角两个点 确定位置和大小 的矩形框)
        axes._set_view_from_bbox将视图范围限制在限位框内
    '''
    last_index = xs[-1]
    # print('last_index:', last_index, type(last_index))
    right, top = plt.gca().transData.transform((last_index, 42))
    left, bottom = plt.gca().axes.transData.transform((last_index - 24*2, 0))
    plt.gca()._set_view_from_bbox(bbox=[left, bottom, right, top])
    
    
    plt.show()
    View Code

      思路:确定一个限位框(由左下角和右上角两个点 确定位置和大小 的矩形框),axes._set_view_from_bbox()将视图范围限制在限位框内

      关键代码:

    1 last_index = xs[-1]
    2 right, top = plt.gca().transData.transform((last_index, 42))
    3 left, bottom = plt.gca().axes.transData.transform((last_index - 24*2, 0))
    4 plt.gca()._set_view_from_bbox(bbox=[left, bottom, right, top])

        第一行:将横轴数据的最后一条存入 last_Index

        第二行:确定限位框的右上边,使用Axes.transData.transform()方法,将用户数据坐标系的点转换为轴域坐标系的点(从用户自己定义的坐标系 转换到 系统使用的像素坐标系)。(last_index, 42) 为 坐标系(xs, ys)中的点, 转换到轴域坐标系为(553.45, 1557.6)。

        第三行:确定限位框的左下边。左边界对应的值为 last_index - 24*2,意为last_index向左移动24个单位长度(单位长度为2, 可以从xs定义中看出来)。

        第四行:将视图显示的范围设为由(left, bottom, right, top)围成的限位框。

        

    • 日期类刻度

        需求: 时间间隔为30分钟, 显示最后24条数据(最近12小时)。

    #!/usr/bin/python
    # _*_ Coding: Utf-8 _*_
    
    import random
    from datetime import datetime
    import matplotlib.dates as mdates
    import matplotlib.pyplot as plt
    import datetime, time
    
    # 假的日期数据
    ex_time = datetime.datetime(2020, 4, 13, 2, 0, 0)  # 起始时间
    ex_timedelta = datetime.timedelta(minutes=30)  # 时间间隔 30min
    date_format = []
    t = [ex_time + i * ex_timedelta for i in range(200)]
    
    # 将格式化的日期(月/日/年 时:分:秒)存入date_format
    for i in t:
        fmt_i = time.strftime('%m/%d/%Y %H:%M:%S', i.timetuple())
        struct_time = datetime.datetime.strptime(fmt_i, '%m/%d/%Y %H:%M:%S')  # strptime()将 字符串转struct_time
        date_format.append(struct_time)
    
    xs = date_format
    ys = [10 + random.randint(1, 8) for i in range(len(xs))]
    
    # 配置横坐标
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
    plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=2))
    plt.gca().xaxis.set_minor_formatter(mdates.DateFormatter('%m/%d/%Y'))
    plt.gca().xaxis.set_minor_locator(mdates.DayLocator())
    
    # 绘制
    plt.plot(xs, ys)
    
    '''
        仅显示最后n个数据
        确定一个限位框(由左下角和右上角两个点 确定位置和大小 的矩形框)
        axes._set_view_from_bbox将视图范围限制在限位框内
    '''
    last_index = xs[-1]
    # print('last_index:', last_index, type(last_index))
    right, top = plt.gca().transData.transform((mdates.date2num(last_index), 42))
    left, bottom = plt.gca().axes.transData.transform((mdates.date2num(last_index) - 1, 0))
    plt.gca()._set_view_from_bbox(bbox=[left, bottom, right, top])
    
    plt.gca().tick_params(axis='x', which='minor', pad=10, color="#27F52F", length=5, width=2)  # 配置刻度属性
    plt.gcf().autofmt_xdate(which='minor')  # 自动旋转日期标记
    
    plt.show()
    View Code

         思路:将日期类刻度转换为数值表示,然后作和数值类刻度一样处理。

         关键代码:

    1 last_index = xs[-1]
    2 right, top = plt.gca().transData.transform((mdates.date2num(last_index), 42))
    3 left, bottom = plt.gca().axes.transData.transform((mdates.date2num(last_index) - 1, 0))
    4 plt.gca()._set_view_from_bbox(bbox=[left, bottom, right, top])

         第一行: 将横轴数据的最后一条存入 last_Index

         第二行:确定限位框的右上边。使用 matplotlib.dates.date2num()方法将日期(datetime.datetime对象)转换为数值,转换方法为:当前日期 - UTC时间 1年1月1日 0时0分0秒 结果的单位为天。

         第三行:确定限位框的左下边。左边界对应的值为 mdates.date2num(last_index) - 1,  意为last_index向左移动一天(单位长度为1,单位为 天 )。

         第四行:将视图显示的范围设为由(left, bottom, right, top)围成的限位框。

  • 相关阅读:
    duilib设置背景颜色透明度
    Centos7 源码编译安装cmake 3.15
    SecureCRT修改背景主题和背景颜色
    fopen的最后一个参数说明
    SFTP从windows上传到linux服务器命令
    小白html 第一个网页
    linux上编译nginx 实现网页开发
    duilib list item互换
    libcurl 错误CURLE_COULDNT_CONNECT 解决办法
    使用mshta.exe绕过应用程序白名单
  • 原文地址:https://www.cnblogs.com/exploer/p/12712501.html
Copyright © 2011-2022 走看看