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

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

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

python多線程實(shí)現(xiàn)動(dòng)態(tài)圖繪制_python

作者:淺若清風(fēng)cyf? ? 更新時(shí)間: 2022-06-15 編程語(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

欄目分類
最近更新