Python 实操显示数据图表并固定时间长度

作者:又是花落时 时间:2021-06-08 02:58:57 

前言:

python利用matplotlib库中的plt.ion()函数实现即时数据动态显示:

1.非定长的时间轴

代码示例:

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import time
from math import *

plt.ion() #开启interactive mode 成功的关键函数
plt.figure(1)
t = [0]
t_now = 0
m = [sin(t_now)]

for i in range(100):
   plt.clf() #清空画布上的所有内容
   t_now = i*0.3
   t.append(t_now)#模拟数据增量流入,保存历史数据
   m.append(sin(t_now))#模拟数据增量流入,保存历史数据
   plt.plot(t,m,'-r')
   plt.draw()#注意此函数需要调用
   plt.pause(0.1)

Python 实操显示数据图表并固定时间长度

此时间轴在不断变长。 

2.定长时间轴 实时显示数据

使用队列  deque,保持数据是定长的,就可以显示固定长度时间轴的动态显示图,

代码示例:

import matplotlib.pyplot as plt
from collections import deque
from math import *
plt.ion()#启动实时
pData = deque(maxlen=30)
for i in range(30):
   pData.append(0)
fig = plt.figure()
t = deque(maxlen=30)
for i in range(30):

t.append(0)
plt.title('Real-time Potentiometer reading')
(l1,)= plt.plot(pData)
plt.ylim([0, 1])
for i in range(2000):
       plt.pause(0.1)#暂停的时间
       t.append(i)
       pData.append(sin(i*0.3))
       print(pData)
       plt.plot(t,pData,'-r')

plt.draw()

Spyder  运行结果(貌似在pycharm 有问题)

Python 实操显示数据图表并固定时间长度

偶然间看到:

import numpy as np
import matplotlib.pyplot as plt

from IPython import display
import math
import time

fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.set_xlabel('Time')
ax.set_ylabel('cos(t)')
ax.set_title('')

line = None
plt.grid(True) #添加网格
plt.ion()  #interactive mode on
obsX = []
obsY = []

t0 = time.time()
while True:
   t = time.time()-t0
   obsX.append(t)
   obsY.append(math.cos(2*math.pi*1*t))

if line is None:
       line = ax.plot(obsX,obsY,'-g',marker='*')[0]

line.set_xdata(obsX)
   line.set_ydata(obsY)

ax.set_xlim([t-10,t+1])
   ax.set_ylim([-1,1])
   plt.pause(0.01)

Python 实操显示数据图表并固定时间长度

来源:https://blog.csdn.net/u013996948/article/details/107040374

标签:Python,数据,图表,固定,长度
0
投稿

猜你喜欢

  • 增强网站的魅力 网页制作技巧三则

    2007-10-04 10:06:00
  • 兼容IE,FF的弹出层登陆界面代码

    2008-01-04 12:13:00
  • Golang之模糊测试工具的使用

    2023-07-01 19:47:47
  • 解决使用OpenCV中的imread()内存报错问题

    2022-06-06 03:14:21
  • python仿抖音表白神器

    2023-03-15 03:35:56
  • 利用一个简单的例子窥探CPython内核的运行机制

    2023-08-11 04:54:31
  • Python区块链创建Genesis Block教程

    2022-04-17 10:44:15
  • 浅谈Python 中的复数问题

    2023-04-22 17:02:11
  • oracle 分页 很棒的sql语句

    2009-07-02 11:44:00
  • Access数据库出现0x80004005问题的解决方法

    2008-11-28 14:25:00
  • python中的np.argmax() 返回最大值索引号

    2022-08-07 14:22:57
  • Python语言实现获取主机名根据端口杀死进程

    2023-07-01 10:05:58
  • Python利用Scrapy框架爬取豆瓣电影示例

    2022-04-30 15:16:17
  • PyPy 如何让Python代码运行得和C一样快

    2022-07-16 20:17:58
  • Python双链表原理与实现方法详解

    2023-10-10 20:55:40
  • Python远程开发环境部署与调试过程图解

    2023-06-07 08:10:40
  • WEB2.0网页制作标准教程(2)什么是名字空间

    2007-11-13 13:04:00
  • Python中命令行参数argparse模块的使用

    2023-11-15 09:25:40
  • Python对口红进行数据分析来选定情人节礼物

    2022-01-23 07:38:08
  • Python常见的几种数据加密方式

    2021-11-18 07:47:04
  • asp之家 网络编程 m.aspxhome.com