三種Matplotlib中動態(tài)更新繪圖的方法總結(jié)
本文展示了如何隨著數(shù)據(jù)的變化動態(tài)更新Matplotlib(Python的數(shù)據(jù)可視化庫)圖。它提供了兩種繪圖方法-第一種是API(適用于大型程序或需要深度控制的程序),第二種是Pyplot接口(受Matlab啟發(fā))。在本文中,我們將展示如何在Pyplot環(huán)境中動態(tài)更新圖。
使用Matplotlib Pyplot繪制線圖
在創(chuàng)建一個動態(tài)更新的圖之前,讓我們首先使用Matplotlib創(chuàng)建/繪制一個簡單的靜態(tài)線圖。此圖稍后將升級為動態(tài)更新數(shù)據(jù)。下面是一個使用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中動態(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ù)時才會繪制繪圖。此外,在show函數(shù)處停止執(zhí)行,直到圖形關(guān)閉。然而,我們可以通過調(diào)用函數(shù).ion()來打開交互模式。當(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更新散點圖的示例
在這個例子中,我們使用“Figure.canvas.draw()”函數(shù)更新matplotlib散點圖。
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中完成動態(tài)更新繪圖的任務(wù)。首先使用matplotlib.animations的FuncAnimation函數(shù),其中定義了更新函數(shù),該函數(shù)在每幀更新數(shù)據(jù)和圖形,其次使用matplotlib交互模式,該模式通過創(chuàng)建更新數(shù)據(jù)的更新循環(huán)來利用圖像在交互模式中即時更新的事實,并在每個周期更新圖形,最后使用“figure.canvas.draw()”方法在每次更新后強制當(dāng)前軸的更新后重新繪制圖形。
到此這篇關(guān)于三種Matplotlib中動態(tài)更新繪圖的方法總結(jié)的文章就介紹到這了,更多相關(guān)Matplotlib動態(tài)繪圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python3 unicode列表轉(zhuǎn)換為中文的實例
今天小編就為大家分享一篇python3 unicode列表轉(zhuǎn)換為中文的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python純代碼通過神經(jīng)網(wǎng)絡(luò)實現(xiàn)線性回歸的擬合方式
這篇文章主要介紹了Python純代碼通過神經(jīng)網(wǎng)絡(luò)實現(xiàn)線性回歸的擬合方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05