Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長度
前言:
python利用matplotlib庫中的plt.ion()函數(shù)實(shí)現(xiàn)即時(shí)數(shù)據(jù)動(dòng)態(tài)顯示:
1.非定長的時(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í)間軸在不斷變長。
2.定長時(shí)間軸 實(shí)時(shí)顯示數(shù)據(jù)
使用隊(duì)列 deque,保持?jǐn)?shù)據(jù)是定長的,就可以顯示固定長度時(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 有問題)
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)
到此這篇關(guān)于Python 實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長度的文章就介紹到這了,更多相關(guān)Python 顯示數(shù)據(jù)圖表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)可視化之使用matplotlib繪制簡單圖表
- Python數(shù)據(jù)可視化之基于pyecharts實(shí)現(xiàn)的地理圖表的繪制
- Python讀取多列數(shù)據(jù)以及用matplotlib制作圖表方法實(shí)例
- Python讀取Excel數(shù)據(jù)并生成圖表過程解析
- Python數(shù)據(jù)可視化 pyecharts實(shí)現(xiàn)各種統(tǒng)計(jì)圖表過程詳解
- python實(shí)現(xiàn)字符串加密 生成唯一固定長度字符串
- python實(shí)現(xiàn)對(duì)指定字符串補(bǔ)足固定長度倍數(shù)截?cái)噍敵龅姆椒?/a>
- python 按照固定長度分割字符串的方法小結(jié)
相關(guān)文章
PyTorch詳解經(jīng)典網(wǎng)絡(luò)ResNet實(shí)現(xiàn)流程
ResNet全稱residual neural network,主要是解決過深的網(wǎng)絡(luò)帶來的梯度彌散,梯度爆炸,網(wǎng)絡(luò)退化(即網(wǎng)絡(luò)層數(shù)越深時(shí),在數(shù)據(jù)集上表現(xiàn)的性能卻越差)的問題2022-05-05
python實(shí)現(xiàn)xlwt xlrd 指定條件給excel行添加顏色
這篇文章主要介紹了python實(shí)現(xiàn)xlwt xlrd 指定條件給excel行添加顏色,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
python time.strptime格式化實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python time.strptime格式化實(shí)例詳解內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02
Python的socket模塊源碼中的一些實(shí)現(xiàn)要點(diǎn)分析
我們平時(shí)引入Python的socket模塊利用其中的方法可以輕松地寫出搭建socket通信的程序,今天我們就來看一下Python的socket模塊源碼中的一些實(shí)現(xiàn)要點(diǎn)分析,領(lǐng)略Python簡潔代碼的一些背后功勞.2016-06-06
一文帶你深入理解Python的`functools.lru_cache`裝飾器
Python中的functools.lru_cache裝飾器是一個(gè)非常有用的裝飾器,它可以幫助我們優(yōu)化遞歸函數(shù),避免重復(fù)計(jì)算已經(jīng)計(jì)算過的值,在這篇文章中,我們將探討?functools.lru_cache?的工作原理以及如何使用它,感興趣的朋友跟著小編一起來學(xué)習(xí)吧2023-07-07

