日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

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

欄目分類
最近更新