三種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ù)來更新繪圖。
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)閉。然而,我們可以通過調(diào)用函數(shù).ion()來打開交互模式。當(dāng)交互模式打開時(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交互模式,該模式通過創(chuàng)建更新數(shù)據(jù)的更新循環(huán)來利用圖像在交互模式中即時(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實(shí)現(xiàn)生成并識(shí)別圖片驗(yàn)證碼
這篇文章主要為大家的詳細(xì)介紹了如何利用Python實(shí)現(xiàn)生成并識(shí)別圖片驗(yàn)證碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02python中enumerate函數(shù)用法實(shí)例分析
這篇文章主要介紹了python中enumerate函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了enumerate函數(shù)的功能、定義及使用技巧,需要的朋友可以參考下2015-05-05python3 unicode列表轉(zhuǎn)換為中文的實(shí)例
今天小編就為大家分享一篇python3 unicode列表轉(zhuǎn)換為中文的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python 編碼Basic Auth使用方法簡(jiǎn)單實(shí)例
這篇文章主要介紹了 Python 編碼Basic Auth使用方法簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05Python純代碼通過神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)線性回歸的擬合方式
這篇文章主要介紹了Python純代碼通過神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)線性回歸的擬合方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05