網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、背景
有些情況下,我們面對(duì)實(shí)時(shí)更新的數(shù)據(jù),希望能夠在一個(gè)窗口中可視化出來,并且能夠?qū)崟r(shí)更新,方便我們觀察數(shù)據(jù)的變化,從而進(jìn)行數(shù)據(jù)分析,例如:繪制音頻的波形,繪制動(dòng)態(tài)曲線等,下面介紹使用matplotlib結(jié)合多線程繪制動(dòng)態(tài)圖,希望能幫助到有需要的朋友。
遇到的場(chǎng)景:最近剛好在學(xué)習(xí)人工智能中的遺傳算法,并且使用該算法求解TSP,了解這個(gè)算法的朋友知道這個(gè)算法是通過不斷迭代,尋找適應(yīng)度大的最優(yōu)解,為了了解迭代過程中適應(yīng)度的變化,我希望能夠?qū)崟r(shí)更新迭代過程中的適應(yīng)度,將其可視化出來(數(shù)據(jù)量不斷增大)
直接上圖:
二、步驟
1、使用matplotlib繪制動(dòng)態(tài)圖
- 工具:matplotlib.animation
2、創(chuàng)建一個(gè)線程用于更新數(shù)據(jù)
- threading
三、代碼框架
# Author: 淺若清風(fēng)cyf
# Date: 2020/12/11
import threading
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.lines as line
import numpy as np
CHUNK = 2048 # 初始數(shù)據(jù)量
data=np.random.normal(0,1,CHUNK) # 存放數(shù)據(jù),用于繪制圖像,數(shù)據(jù)類型可為列表
# 定義畫布
fig = plt.figure()
ax = plt.subplot(111,ylim=(0,5))
line = line.Line2D([], []) # 繪制直線
# 初始化圖像
def plot_init():
ax.add_line(line)
return line, # 必須加逗號(hào),否則會(huì)報(bào)錯(cuò)(TypeError: 'Line2D' object is not iterable)
# 更新圖像(animation會(huì)不斷調(diào)用此函數(shù)刷新圖像,實(shí)現(xiàn)動(dòng)態(tài)圖的效果)
def plot_update(i):
global data # data為全局變量
data_copy = data.copy() # 為避免線程不同步導(dǎo)致獲取到的data在繪制圖像時(shí)被更新,這里復(fù)制數(shù)據(jù)的副本,否則繪制圖像的時(shí)候可能會(huì)出現(xiàn)x和y的數(shù)據(jù)維度不相等的情況
x_data=np.arange(0,data_copy.shape[0],1) # x軸根據(jù)y軸數(shù)據(jù)自動(dòng)生成(可根據(jù)需要修改)
ax.set_xlim(0,data_copy.shape[0]) # 橫坐標(biāo)范圍(橫坐標(biāo)的范圍和刻度可根據(jù)數(shù)據(jù)長(zhǎng)度更新)
ax.set_title("title",fontsize=8) # 設(shè)置title
line.set_xdata(x_data) # 更新直線的數(shù)據(jù)
line.set_ydata(data_copy) # 更新直線的數(shù)據(jù)
# 大標(biāo)題(若有多個(gè)子圖,可為其設(shè)置大標(biāo)題)
plt.suptitle('Suptitle',fontsize=8)
# 重新渲染子圖
ax.figure.canvas.draw() # 必須加入這一行代碼,才能更新title和坐標(biāo)!!!
return line, # 必須加逗號(hào),否則會(huì)報(bào)錯(cuò)(TypeError: 'Line2D' object is not iterable)
# 繪制動(dòng)態(tài)圖
ani = animation.FuncAnimation(fig, # 畫布
plot_update, # 圖像更新
init_func=plot_init, # 圖像初始化
frames=1,
interval=30, # 圖像更新間隔
blit=True)
# 數(shù)據(jù)更新函數(shù)
def dataUpdate_thead():
global data
# 為了方便理解代碼,這里生成正態(tài)分布的隨機(jī)數(shù)據(jù)
while True: # 為了方便測(cè)試,讓數(shù)據(jù)不停的更新
data=np.random.normal(0,1,CHUNK)
# 為數(shù)據(jù)更新函數(shù)單獨(dú)創(chuàng)建一個(gè)線程,與圖像繪制的線程并發(fā)執(zhí)行
ad_rdy_ev = threading.Event()
ad_rdy_ev.set() # 設(shè)置線程運(yùn)行
t = threading.Thread(target=dataUpdate_thead, args=()) # 更新數(shù)據(jù),參數(shù)說明:target是線程需要執(zhí)行的函數(shù),args是傳遞給函數(shù)的參數(shù))
t.daemon = True
t.start() # 線程執(zhí)行
plt.show() # 顯示圖像(0,1,CHUNK)
# 為數(shù)據(jù)更新函數(shù)單獨(dú)創(chuàng)建一個(gè)線程,與圖像繪制的線程并發(fā)執(zhí)行
ad_rdy_ev = threading.Event()
ad_rdy_ev.set() # 設(shè)置線程運(yùn)行
t = threading.Thread(target=dataUpdate_thead, args=()) # 更新數(shù)據(jù),參數(shù)說明:target是線程需要執(zhí)行的函數(shù),args是傳遞給函數(shù)的參數(shù))
t.daemon = True
t.start() # 線程執(zhí)行
plt.show() # 顯示圖像
效果:
原文鏈接:https://juejin.cn/post/7085002428495429668
相關(guān)推薦
- 2023-04-06 分布式系統(tǒng)CAP定理中的P原理解析_相關(guān)技巧
- 2022-10-12 詳解C++異常處理機(jī)制示例介紹_C 語(yǔ)言
- 2022-06-13 docker鏡像的拉取登陸上傳及保存等相關(guān)使用命令_docker
- 2022-07-14 Python?socket如何實(shí)現(xiàn)服務(wù)端和客戶端數(shù)據(jù)傳輸(TCP)_python
- 2021-12-11 Redis線程模型的原理分析_Redis
- 2022-12-01 Python?Flask前端自動(dòng)登錄功能實(shí)現(xiàn)詳解_python
- 2024-01-12 com.fasterxml.jackson.databind.exc.InvalidDefiniti
- 2022-07-14 Matlab實(shí)現(xiàn)灰色預(yù)測(cè)的示例代碼_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支