python中plot實現(xiàn)即時數(shù)據(jù)動態(tài)顯示方法
在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)
示例代碼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))
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于python通過新建環(huán)境安裝tfx的問題
這篇文章主要介紹了python安裝tfx/新建環(huán)境,新建一個環(huán)境tfx專門用來運行流水線,這個環(huán)境安裝python3.8,對python安裝tfx相關(guān)知識感興趣的朋友一起看看吧2022-05-05源碼解析python中randint函數(shù)的效率缺陷
這篇文章主要介紹了源碼解析python中randint函數(shù)的效率缺陷,通過討論?random?模塊的實現(xiàn),并討論了一些更為快速的生成偽隨機整數(shù)的替代方法展開主題,需要的盆友可以參考一下2022-06-06PyQT實現(xiàn)菜單中的復(fù)制,全選和清空的功能的方法
今天小編就為大家分享一篇PyQT實現(xiàn)菜單中的復(fù)制,全選和清空的功能的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06PyTorch零基礎(chǔ)入門之構(gòu)建模型基礎(chǔ)
PyTorch是一個開源的Python機器學習庫,基于Torch,用于自然語言處理等應(yīng)用程序,它是一個可續(xù)計算包,提供兩個高級功能:1、具有強大的GPU加速的張量計算(如NumPy)。2、包含自動求導(dǎo)系統(tǒng)的深度神經(jīng)網(wǎng)絡(luò)2021-10-10Python 基于 pygame 實現(xiàn)輪播圖動畫效果
在Python中可以適應(yīng)第三方庫pygame來實現(xiàn)輪播圖動畫的效果,使用pygame前需確保其已經(jīng)安裝,本文通過實例代碼介紹Python 基于 pygame 實現(xiàn)輪播圖動畫效果,感興趣的朋友跟隨小編一起看看吧2024-03-03