溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python中plot實現(xiàn)即時數(shù)據(jù)動態(tài)顯示方法

發(fā)布時間:2020-10-22 16:58:47 來源:腳本之家 閱讀:663 作者:_風(fēng)起了_ 欄目:開發(fā)技術(shù)

在Matlab使用Plot函數(shù)實現(xiàn)數(shù)據(jù)動態(tài)顯示方法總結(jié)中介紹了兩種實現(xiàn)即時數(shù)據(jù)動態(tài)顯示的方法??紤]到使用python的人群日益增多,再加上本人最近想使用python動態(tài)顯示即時的數(shù)據(jù),網(wǎng)上方法很少,固總結(jié)于此。

示例代碼1

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(2000):
 t_now = i*0.1
 t.append(t_now)#模擬數(shù)據(jù)增量流入
 m.append(sin(t_now))#模擬數(shù)據(jù)增量流入
 plt.plot(t,m,'-r')
 plt.draw()#注意此函數(shù)需要調(diào)用
 time.sleep(0.01)

python中plot實現(xiàn)即時數(shù)據(jù)動態(tài)顯示方法

示例代碼2

上面的方式,可以在跳出的畫圖面板內(nèi)動態(tài)顯示,但是如果想在jupyter notebook中直接動態(tài)顯示,上面的方法將無效。因此,補上在jupyter notebook中可行的動態(tài)顯示示例程序。以供舉一反三之用。
這里寫代碼片

import math
import random
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

# set up matplotlib
is_ipython = 'inline' in matplotlib.get_backend()
if is_ipython:
 from IPython import display

plt.ion()

def plot_durations(y):
 plt.figure(2)
 plt.clf()
 plt.subplot(211)
 plt.plot(y[:,0])
 plt.subplot(212)
 plt.plot(y[:,1])

 plt.pause(0.001) # pause a bit so that plots are updated
 if is_ipython:
  display.clear_output(wait=True)
  display.display(plt.gcf())


x = np.linspace(-10,10,500)
y = []
for i in range(len(x)):
 y1 = np.cos(i/(3*3.14))
 y2 = np.sin(i/(3*3.14))
 y.append(np.array([y1,y2]))
 plot_durations(np.array(y))

python中plot實現(xiàn)即時數(shù)據(jù)動態(tài)顯示方法

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI