亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

本文展示了如何隨著數(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爬蟲中異步協(xié)程的用法

    python3爬蟲中異步協(xié)程的用法

    在本篇文章里小編給大家整理的是關(guān)于python3爬蟲中異步協(xié)程的用法,需要的朋友們可以學(xué)習(xí)參考下。
    2020-07-07
  • Python干貨:分享Python繪制六種可視化圖表

    Python干貨:分享Python繪制六種可視化圖表

    可視化圖表有很多種,這篇文章主要介紹了Python繪制六種可視化圖表詳解的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • Python繪制土地利用和土地覆蓋類型圖示例詳解

    Python繪制土地利用和土地覆蓋類型圖示例詳解

    本文介紹了如何使用Python繪制土地利用和土地覆蓋類型圖,并提供了詳細(xì)的代碼示例,通過安裝所需的庫,準(zhǔn)備地理數(shù)據(jù),使用geopandas和matplotlib等庫,可以繪制出土地利用和覆蓋類型圖,感興趣的朋友一起看看吧
    2025-01-01
  • Pycharm漢化兩種方法(pycharm改為中文版)

    Pycharm漢化兩種方法(pycharm改為中文版)

    PyCharm是一款流行的Python開發(fā)環(huán)境,提供了豐富的功能和工具,使得Python的開發(fā)和調(diào)試變得更加方便和高效,下面這篇文章主要給大家介紹了Pycharm漢化的兩種方法,所謂漢化就是將pycharm改為中文版,需要的朋友可以參考下
    2023-06-06
  • 利用Python實現(xiàn)生成并識別圖片驗證碼

    利用Python實現(xiàn)生成并識別圖片驗證碼

    這篇文章主要為大家的詳細(xì)介紹了如何利用Python實現(xiàn)生成并識別圖片驗證碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • 使用Requests庫來進行爬蟲的方式

    使用Requests庫來進行爬蟲的方式

    這篇文章主要介紹了使用Requests庫來進行爬蟲的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python中enumerate函數(shù)用法實例分析

    python中enumerate函數(shù)用法實例分析

    這篇文章主要介紹了python中enumerate函數(shù)用法,以實例形式較為詳細(xì)的分析了enumerate函數(shù)的功能、定義及使用技巧,需要的朋友可以參考下
    2015-05-05
  • python3 unicode列表轉(zhuǎn)換為中文的實例

    python3 unicode列表轉(zhuǎn)換為中文的實例

    今天小編就為大家分享一篇python3 unicode列表轉(zhuǎn)換為中文的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python 編碼Basic Auth使用方法簡單實例

    Python 編碼Basic Auth使用方法簡單實例

    這篇文章主要介紹了 Python 編碼Basic Auth使用方法簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Python純代碼通過神經(jīng)網(wǎng)絡(luò)實現(xiàn)線性回歸的擬合方式

    Python純代碼通過神經(jīng)網(wǎng)絡(luò)實現(xiàn)線性回歸的擬合方式

    這篇文章主要介紹了Python純代碼通過神經(jīng)網(wǎng)絡(luò)實現(xiàn)線性回歸的擬合方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評論