網(wǎng)站首頁 編程語言 正文
前言:
python利用matplotlib庫中的plt.ion()
函數(shù)實現(xiàn)即時數(shù)據(jù)動態(tài)顯示:
1.非定長的時間軸
代碼示例:
# -*- 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)
此時間軸在不斷變長。?
2.定長時間軸 實時顯示數(shù)據(jù)
使用隊列? deque,保持數(shù)據(jù)是定長的,就可以顯示固定長度時間軸的動態(tài)顯示圖,
代碼示例:
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? 運行結(jié)果(貌似在pycharm 有問題)
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)推薦
- 2024-03-23 如何保證Redis和數(shù)據(jù)庫數(shù)據(jù)一致性
- 2022-06-16 c語言單詞搜索的實現(xiàn)_C 語言
- 2022-06-08 FreeRTOS實時操作系統(tǒng)支持時間片示例詳解_操作系統(tǒng)
- 2022-07-26 golang控制goroutine數(shù)量以及獲取處理結(jié)果
- 2022-03-26 C語言輸出孿生素數(shù)的實現(xiàn)示例_C 語言
- 2022-10-05 Ubuntu?Server?20.04?LTS?環(huán)境下搭建vim?編輯器Python?IDE的詳細步
- 2022-05-24 Matplotlib實現(xiàn)各種條形圖繪制_python
- 2023-02-12 python中使用docx模塊處理word文檔_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支