網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長(zhǎng)度_python
作者:又是花落時(shí) ? 更新時(shí)間: 2022-10-17 編程語(yǔ)言前言:
python利用matplotlib庫(kù)中的plt.ion()
函數(shù)實(shí)現(xiàn)即時(shí)數(shù)據(jù)動(dòng)態(tài)顯示:
1.非定長(zhǎng)的時(shí)間軸
代碼示例:
# -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np import time from math import * plt.ion() #開啟interactive mode 成功的關(guān)鍵函數(shù) plt.figure(1) t = [0] t_now = 0 m = [sin(t_now)] for i in range(100): plt.clf() #清空畫布上的所有內(nèi)容 t_now = i*0.3 t.append(t_now)#模擬數(shù)據(jù)增量流入,保存歷史數(shù)據(jù) m.append(sin(t_now))#模擬數(shù)據(jù)增量流入,保存歷史數(shù)據(jù) plt.plot(t,m,'-r') plt.draw()#注意此函數(shù)需要調(diào)用 plt.pause(0.1)
此時(shí)間軸在不斷變長(zhǎng)。?
2.定長(zhǎng)時(shí)間軸 實(shí)時(shí)顯示數(shù)據(jù)
使用隊(duì)列? deque,保持?jǐn)?shù)據(jù)是定長(zhǎng)的,就可以顯示固定長(zhǎng)度時(shí)間軸的動(dòng)態(tài)顯示圖,
代碼示例:
import matplotlib.pyplot as plt from collections import deque from math import * plt.ion()#啟動(dòng)實(shí)時(shí) 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)#暫停的時(shí)間 t.append(i) pData.append(sin(i*0.3)) print(pData) plt.plot(t,pData,'-r') plt.draw()
Spyder? 運(yùn)行結(jié)果(貌似在pycharm 有問(wèn)題)
s?
偶然間看到:
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) #添加網(wǎng)格 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)
原文鏈接:https://blog.csdn.net/u013996948/article/details/107040374
相關(guān)推薦
- 2023-07-09 Go語(yǔ)言new與make區(qū)別
- 2023-10-25 用promise.all搭配map方法解決異步問(wèn)題
- 2022-01-29 fastadmin uniapp跨域的問(wèn)題
- 2022-11-07 Go語(yǔ)言深度拷貝工具deepcopy的使用教程_Golang
- 2022-06-22 使用docker?compose一鍵部署WordPress博客的方法_docker
- 2022-03-29 Android頂部標(biāo)題欄的布局設(shè)計(jì)_Android
- 2023-12-11 使用SSH地址拉取遠(yuǎn)程倉(cāng)庫(kù)代碼報(bào)下面的錯(cuò)誤
- 2022-09-24 教你創(chuàng)建一個(gè)帶診斷工具的.NET鏡像_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支