三種Matplotlib中動(dòng)態(tài)更新繪圖的方法總結(jié)
本文展示了如何隨著數(shù)據(jù)的變化動(dòng)態(tài)更新Matplotlib(Python的數(shù)據(jù)可視化庫(kù))圖。它提供了兩種繪圖方法-第一種是API(適用于大型程序或需要深度控制的程序),第二種是Pyplot接口(受Matlab啟發(fā))。在本文中,我們將展示如何在Pyplot環(huán)境中動(dòng)態(tài)更新圖。
使用Matplotlib Pyplot繪制線圖
在創(chuàng)建一個(gè)動(dòng)態(tài)更新的圖之前,讓我們首先使用Matplotlib創(chuàng)建/繪制一個(gè)簡(jiǎn)單的靜態(tài)線圖。此圖稍后將升級(jí)為動(dòng)態(tài)更新數(shù)據(jù)。下面是一個(gè)使用Matplotlib創(chuàng)建靜態(tài)線圖的程序。
import matplotlib.pyplot as plt x = [1,2,3,4] # x-coordinates of the data points y = [4,7,6,8] # y-coordinates of the data points graph = plt.plot(x,y) # plotting the data and storing the graph in variable named graph plt.show() # showing the resultant graph

在Matplotlib中動(dòng)態(tài)更新繪圖
1.使用matplotlib.animations
我們可以使用“matplotlib.animations.FuncAnimation”函數(shù)來(lái)更新繪圖。
from matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt import random # initial data x = [1] y = [random.randint(1,10)] # creating the first plot and frame fig, ax = plt.subplots() graph = ax.plot(x,y,color = 'g')[0] plt.ylim(0,10) # updates the data and graph def update(frame): global graph # updating the data x.append(x[-1] + 1) y.append(random.randint(1,10)) # creating a new graph or updating the graph graph.set_xdata(x) graph.set_ydata(y) plt.xlim(x[0], x[-1]) anim = FuncAnimation(fig, update, frames = None) plt.show()

2.使用pyplot交互模式更新Matplotlib圖
默認(rèn)情況下,交互模式是關(guān)閉的,因此只有在調(diào)用show函數(shù)時(shí)才會(huì)繪制繪圖。此外,在show函數(shù)處停止執(zhí)行,直到圖形關(guān)閉。然而,我們可以通過(guò)調(diào)用函數(shù).ion()來(lái)打開(kāi)交互模式。當(dāng)交互模式打開(kāi)時(shí),圖形會(huì)立即繪制,并在我們對(duì)其進(jìn)行任何更改時(shí)立即更新。我們可以使用此行為使用以下方法動(dòng)態(tài)更新繪圖
import matplotlib.pyplot as plt import random plt.ion() # turning interactive mode on # preparing the data y = [random.randint(1,10) for i in range(20)] x = [*range(1,21)] # plotting the first frame graph = plt.plot(x,y)[0] plt.ylim(0,10) plt.pause(1) # the update loop while(True): # updating the data y.append(random.randint(1,10)) x.append(x[-1]+1) # removing the older graph graph.remove() # plotting newer graph graph = plt.plot(x,y,color = 'g')[0] plt.xlim(x[0], x[-1]) # calling pause function for 0.25 seconds plt.pause(0.25)

3.Matplotlib更新散點(diǎn)圖的示例
在這個(gè)例子中,我們使用“Figure.canvas.draw()”函數(shù)更新matplotlib散點(diǎn)圖。
import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import random # initial data x = [random.randint(1,100)] y = [random.randint(1,100)] # creating the figure and axes object fig, ax = plt.subplots() # update function to update data and plot def update(frame): # updating the data by adding one more point x.append(random.randint(1,100)) y.append(random.randint(1,100)) ax.clear() # clearing the axes ax.scatter(x,y, s = y, c = 'b', alpha = 0.5) # creating new scatter chart with updated data fig.canvas.draw() # forcing the artist to redraw itself anim = FuncAnimation(fig, update) plt.show()

總結(jié)
至少有3種方法可以在matplotlib中完成動(dòng)態(tài)更新繪圖的任務(wù)。首先使用matplotlib.animations的FuncAnimation函數(shù),其中定義了更新函數(shù),該函數(shù)在每幀更新數(shù)據(jù)和圖形,其次使用matplotlib交互模式,該模式通過(guò)創(chuàng)建更新數(shù)據(jù)的更新循環(huán)來(lái)利用圖像在交互模式中即時(shí)更新的事實(shí),并在每個(gè)周期更新圖形,最后使用“figure.canvas.draw()”方法在每次更新后強(qiáng)制當(dāng)前軸的更新后重新繪制圖形。
到此這篇關(guān)于三種Matplotlib中動(dòng)態(tài)更新繪圖的方法總結(jié)的文章就介紹到這了,更多相關(guān)Matplotlib動(dòng)態(tài)繪圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中不可錯(cuò)過(guò)的五個(gè)超有用函數(shù)
在本文中,我們用代碼詳細(xì)說(shuō)明了Python中超實(shí)用的5個(gè)函數(shù)的重要作用,這些函數(shù)雖然簡(jiǎn)單,但卻是Python中功能最強(qiáng)大的函數(shù),下面一起來(lái)看看文章的詳細(xì)介紹吧,希望對(duì)你的學(xué)習(xí)有所幫助2022-01-01
使用Python批量合并多個(gè)Excel文件的代碼實(shí)現(xiàn)
本文介紹如何使用Python批量合并多個(gè)Excel文件,通過(guò)定義函數(shù),可以輕松地將指定文件夾內(nèi)的所有Excel文件整合到一個(gè)文件中,并提供了按文件名提取日期信息及按原文件名區(qū)分Sheet的方法,需要的朋友可以參考下2025-12-12
Python操作Elasticsearch處理timeout超時(shí)
這篇文章主要介紹了Python操作Elasticsearch處理timeout超時(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境的教程圖解
這篇文章主要介紹了Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例,排序算法、查找算法和圖遍歷算法是計(jì)算機(jī)科學(xué)中常見(jiàn)且重要的算法。它們?cè)跀?shù)據(jù)處理、搜索和圖結(jié)構(gòu)等領(lǐng)域發(fā)揮著關(guān)鍵作用,需要的朋友可以參考下2023-08-08
如何基于python實(shí)現(xiàn)單目三維重建詳解
單目三維重建是根據(jù)單個(gè)攝像頭的運(yùn)動(dòng)模擬雙目視覺(jué)獲得物體在空間中的三維視覺(jué)信息,下面這篇文章主要給大家介紹了關(guān)于如何基于python實(shí)現(xiàn)單目三維重建的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

