欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

三種Matplotlib中動(dòng)態(tài)更新繪圖的方法總結(jié)

 更新時(shí)間:2024年04月09日 08:38:25   作者:python收藏家  
這篇文章主要為大家詳細(xì)介紹了如何隨著數(shù)據(jù)的變化動(dòng)態(tài)更新Matplotlib(Python的數(shù)據(jù)可視化庫(kù))圖,文中介紹了常用的三種方法,希望對(duì)大家有所幫助

本文展示了如何隨著數(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)文章

最新評(píng)論