matplotlib實現(xiàn)數(shù)據(jù)實時刷新的示例代碼
前言
matplotlib是python下非常好用的一個數(shù)據(jù)可視化套件,網(wǎng)上相關的教程也非常豐富,使用方便。本人需求一個根據(jù)實時數(shù)據(jù)刷新曲線的上位機軟件,找了半天,基本上都是使用matplotlib的交互模式,我折騰半天還是沒有實現(xiàn)想要的效果,但卻通過另一種方法實現(xiàn)了想要的效果。
源碼
注釋已經(jīng)很充分,不多贅述,直接看源碼。
import matplotlib.pyplot as plt import numpy as np import threading import sys from random import random, randrange from time import sleep ''' 繪制2x2的畫板 可設置窗口標題和4個子圖標題 可更新曲線數(shù)據(jù) ''' quit_flag = False # 退出標志 class Plot2_2(object): """ 2x2的畫板 """ def __init__(self, wtitle='Figure', p1title='1', p2title='2', p3title='3', p4title='4'): self.sub_title = [p1title, p2title, p3title, p4title] # 4個子圖的標題 self.fig, self.ax = plt.subplots(2, 2) # 創(chuàng)建2X2子圖 self.fig.subplots_adjust(wspace=0.3, hspace=0.3) # 設置子圖之間的間距 self.fig.canvas.set_window_title(wtitle) # 設置窗口標題 # 子圖字典,key為子圖的序號,value為子圖句柄 self.axdict = {0: self.ax[0, 0], 1: self.ax[0, 1], 2: self.ax[1, 0], 3: self.ax[1, 1]} def showPlot(self): """ 顯示曲線 """ plt.show() def setPlotStyle(self, index): """ 設置子圖的樣式,這里僅設置了標題 """ self.axdict[index].set_title(self.sub_title[index], fontsize=12) def updatePlot(self, index, x, y): """ 更新指定序號的子圖 :param index: 子圖序號 :param x: 橫軸數(shù)據(jù) :param y: 縱軸數(shù)據(jù) :return: """ # X軸數(shù)據(jù)必須和Y軸數(shù)據(jù)長度一致 if len(x) != len(y): ex = ValueError("x and y must have same first dimension") raise ex self.axdict[index].cla() # 清空子圖數(shù)據(jù) self.axdict[index].plot(x, y) # 繪制最新的數(shù)據(jù) self.setPlotStyle(index) # 設置子圖樣式 if min(x) < max(x): self.axdict[index].set_xlim(min(x), max(x)) # 根據(jù)X軸數(shù)據(jù)區(qū)間調(diào)整X軸范圍 plt.draw() print("%s end" % sys._getframe().f_code.co_name) def updatePlot(plot): """ 模擬收到實時數(shù)據(jù),更新曲線的操作 :param plot: 曲線實例 :return: """ print("Thread: %s" % threading.current_thread().getName()) count = 0 global quit_flag print("quit_flag[%s]" % str(quit_flag)) while True: if quit_flag: print("quit_flag[%s]" % str(quit_flag)) break count += 1 print("count#%d" % count) x = np.arange(0, 100, 1) y = np.random.normal(loc=1, scale=1, size=100) # 產(chǎn)生隨機數(shù),模擬變化的曲線 index = randrange(4) # 隨機更新某一個子圖 plot.updatePlot(index, x, y) sleep(random() * 3) def main(): p = Plot2_2() # 創(chuàng)建一個2X2畫板 t = threading.Thread(target=updatePlot, args=(p,)) # 啟動一個線程更新曲線數(shù)據(jù) t.start() p.showPlot() # showPlot方法會阻塞當前線程,直到窗口關閉 print("plot close") global quit_flag quit_flag = True # 通知更新曲線數(shù)據(jù)的線程退出 t.join() print("Thread: %s end" % threading.current_thread().getName()) if __name__ == '__main__': main()
結(jié)語
上述方法初步實現(xiàn)了根據(jù)實時數(shù)據(jù)刷新曲線的效果,目前測試發(fā)現(xiàn)偶爾程序無法完全退出,還有待改進。到此這篇關于matplotlib實現(xiàn)數(shù)據(jù)實時刷新的示例代碼的文章就介紹到這了,更多相關matplotlib 數(shù)據(jù)實時刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用BeautifulSoup4解析XML的方法小結(jié)
這篇文章主要介紹了使用BeautifulSoup4解析XML的方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12Django?報錯:Broken?pipe?from?('127.0.0.1',?5892
這篇文章主要介紹了Django?報錯:Broken?pipe?from?('127.0.0.1',?58924)的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09pytorch1.60 torch.nn在pycharm中無法自動智能提示的解決
這篇文章主要介紹了pytorch1.60 torch.nn在pycharm中無法自動智能提示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Python 安裝第三方庫 pip install 安裝慢安裝不上的解決辦法
很多朋友反映在使用pip install安裝python 第三方庫的過程中會出現(xiàn)網(wǎng)速很慢,或者是安裝下載到中途,停止,卡主,或者是下載報錯等問題,下面小編給大家?guī)砹私鉀Q方法,一起看看吧2019-06-06淺談python中常用的8種經(jīng)典數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了python中常用的8種經(jīng)典數(shù)據(jù)結(jié)構(gòu),包括原生數(shù)據(jù)結(jié)構(gòu),NumPy包中的數(shù)據(jù)結(jié)構(gòu),以及Pandas包中的數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2023-03-03Python打印scrapy蜘蛛抓取樹結(jié)構(gòu)的方法
這篇文章主要介紹了Python打印scrapy蜘蛛抓取樹結(jié)構(gòu)的方法,實例分析了打印scrapy蜘蛛抓取樹結(jié)構(gòu)的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04