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()

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

  关键代码:

 last_index = xs[-1]
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])

    第一行:将横轴数据的最后一条存入 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()

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

     关键代码:

 last_index = xs[-1]
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])

     第一行: 将横轴数据的最后一条存入 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)围成的限位框。

matplotlib 显示最后n条数据(可用于实时更新)的更多相关文章

  1. mysql根据查询结果批量更新多条数据(插入或更新)

    mysql根据查询结果批量更新多条数据(插入或更新) 1.1 前言 mysql根据查询结果执行批量更新或插入时经常会遇到1093的错误问题.基本上批量插入或新增都会涉及到子查询,mysql是建议不要对 ...

  2. Python tkinter库将matplotlib图表显示在GUI窗口上,并实时更新刷新数据

    代码 1 ''' 2 使用matplotlib创建图表,并显示在tk窗口 3 ''' 4 import matplotlib.pyplot as plt 5 from matplotlib.pylab ...

  3. Decoration2:引入Angularjs显示前台一条数据

    SpringMVC内置的RestFul API格式采用的是最复杂最全面的HATEOAS规范,对于简单应用来说,前台解析起来不方便,我们下面主要想办法重新定义一种简单的RestFulAPI. (1)先是 ...

  4. 只显示前几条数据的sql语句写法 七种数据库中Select Top的使用方法

    七种数据库中Select Top的使用方法 1. Oracle数据库 SELECT * FROM TABLENAME WHERE ROWNUM <= N 2. Infomix数据库 SELECT ...

  5. 过千万、亿条数据的mysql表更新 mysql 线程状态

    分段更新 UPDATE question SET `status`=1 WHERE status!=1 LIMIT 3000;UPDATE answer SET `status`=1 WHERE st ...

  6. Dev GridControl数据修改后实时更新数据源

      1:  /// <summary> 2:  /// 嵌入的ComboxEdit控件选择值变化事件 3:  /// </summary> 4: /// <param n ...

  7. Dev GridControl数据修改后实时更新数据源(转)

    1:  /// <summary> 2:  /// 嵌入的ComboxEdit控件选择值变化事件 3:  /// </summary> 4: /// <param nam ...

  8. vue实现两重列表集合,点击显示,点击隐藏的折叠效果,(默认显示集合最新一条数据,点击展开,显示集合所有数据)

    效果图: 默认显示最新一条数据: 点击显示所有数据: 代码: 说明:这里主要是 这块用来控制显示或者隐藏 根据当前点击的  这个方法里传递的index 对应  isShow 数组里的index  ,对 ...

  9. QTreeView处理大量数据(使用1000万条数据,每次都只是部分刷新)

    如何使QTreeView快速显示1000万条数据,并且内存占用量少呢?这个问题困扰我很久,在网上找了好多相关资料,都没有找到合理的解决方案,今天在这里把我的解决方案提供给朋友们,供大家相互学习. 我开 ...

随机推荐

  1. 微信小程序开发(一)开发工具推荐VSCode

    虽然微信小程序官方开发工具非常优秀,但用的时间久了,会发现一些问题,比如代码编辑区小,自定义能力差,不支持插件,有时还会出现莫名其妙的bug,最不能忍的是编辑器代码提示功能不健全,这对于新手来说,很不 ...

  2. php 调用curl_init失败

    当你在开发微信公众号,微信小程序的时候,往往会遇到困难 进入服务器,输入 tail -f /var/log/apache2/error.log 看看apache2的日志 就因为php 的curl扩展没 ...

  3. 如何用 Blazor 实现 Ant Design 组件库

    本文主要分享我创建 Ant Design of Blazor 项目的心路历程,已经文末有一个 Blazor 线上分享预告. Blazor WebAssembly 来了! Blazor 这个新推出的前端 ...

  4. Journal of Proteome Research | SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants for proteogenomic interpretation | SAAV的识别、功能注释和检索 | (解读人:徐洪凯)

    文献名:SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants fo ...

  5. 201771010111-李瑞红 实验一 软件工程准备-<构建之法-现代软件工程-基础认识和理解>

    |||||||| | :--

  6. Java流中的map算子和flatMap算子的区别

    map算子和flatMap算子 map和flatMap都是映射(转换),那么他们之间究竟有什么区别呢? 1.我们先简单了解下map算子: @org.junit.Test public void tes ...

  7. Smallest Range II

    2020-01-21 21:43:52 问题描述: 问题求解: 这个题目还是有点难度的,感觉很巧妙也很难想到. 整体的思路如下: 1. 首先原问题等价于 +0 / + 2*K 2. 那么res = M ...

  8. Nginx做负载均衡的几种轮询策略

    集群环境为了解决单点无法支撑高并发的情况,集群采用多台服务器提供服务,一般在集群中使用nginx 将来自客户端的请求转发给服务器端 nginx负载均衡可用提高网站的吞吐量,缓解单台服务器的压力. 一. ...

  9. 不同label样本画图——颜色分配plt.cm.Spectral

    不同label样本画图——颜色分配plt.cm.Spectralhttps://blog.csdn.net/wang_zuel/article/details/102940092 关于plt.cm.S ...

  10. 福利,OpenCV最新中文版官方教程来了

    OpenCV 中文版官方教程来了. OpenCV是计算机视觉中经典的专用库,然而其中文版官方教程久久不来.近日,一款最新OpenCV4.1 版本的完整中文版官方教程出炉,读者朋友可以更好的学习了解Op ...