最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用matplotlib實(shí)現(xiàn)動(dòng)態(tài)可視化詳解

 更新時(shí)間:2023年08月28日 14:56:27   作者:python收藏家  
Python中的數(shù)據(jù)可視化是指原始數(shù)據(jù)的圖形表示,以更好地可視化、理解和推理,Python提供了各種庫,包含用于可視化數(shù)據(jù)的不同特性,下面我們就來看看如何利用matplotlib實(shí)現(xiàn)動(dòng)態(tài)可視化吧

Python中的數(shù)據(jù)可視化是指原始數(shù)據(jù)的圖形表示,以更好地可視化、理解和推理。Python提供了各種庫,包含用于可視化數(shù)據(jù)的不同特性,并且可以支持不同類型的圖形,即Matplotlib、Seaborn、Bokeh、Plotly等。

Python中的動(dòng)態(tài)可視化

任何系統(tǒng)的動(dòng)態(tài)可視化都意味著在演示過程中以圖形方式表示當(dāng)前系統(tǒng)的狀態(tài)變化。在Python中的數(shù)據(jù)可視化方面,動(dòng)態(tài)可視化是一個(gè)動(dòng)態(tài)圖形,它要么隨著時(shí)間的推移而變化,就像在視頻中一樣,要么隨著用戶改變輸入而變化,但在當(dāng)前演示中,它就像是活的一樣。

在Python中創(chuàng)建動(dòng)態(tài)圖的步驟

步驟1. 創(chuàng)建固定長度的隊(duì)列

隊(duì)列是一種線性數(shù)據(jù)結(jié)構(gòu),以先進(jìn)先出(FIFO)原則存儲(chǔ)項(xiàng)目。它可以在Python中以各種方式實(shí)現(xiàn)。在Python中創(chuàng)建一個(gè)固定長度的隊(duì)列用于動(dòng)態(tài)繪圖是指創(chuàng)建一個(gè)數(shù)據(jù)結(jié)構(gòu),該結(jié)構(gòu)可以存儲(chǔ)固定數(shù)量的元素,并在容器滿時(shí)丟棄最舊的元素。當(dāng)我們要繪制不斷更新的數(shù)據(jù)點(diǎn)時(shí),它非常有用。通過限制容器的大小,可以提高繪制的性能和清晰度。

from collections import deque
# Create a deque with a maximum length of 3
data_points = deque(maxlen=3)
# Add new data points to the deque
data_points.append(40)
data_points.append(60)
data_points.append(30)
# display the data points in the deque
print(data_points)  # deque([40, 60, 30], maxlen=3)
# Add more data points to the deque
data_points.append(13)
data_points.append(99)
# The oldest data point is/are automatically 
# removed from front of queue.
# deque([30, 13, 99], maxlen=3)
print(data_points)  

輸出

deque([40, 60, 30], maxlen=3)
deque([30, 13, 99], maxlen=3)

步驟2. 生成點(diǎn)并將其保存到隊(duì)列

在這里,我們動(dòng)態(tài)地生成數(shù)據(jù)點(diǎn),并將它們附加到隊(duì)列數(shù)據(jù)結(jié)構(gòu)中,而不是像上面的示例中所示的那樣執(zhí)行手動(dòng)操作。這里我們將使用Python的random模塊中可用的函數(shù)來生成數(shù)據(jù)點(diǎn)。

from collections import deque
import random
# Create a deque with fixed length 5
data_points = deque(maxlen=5)
# Generate and append data points to the deque
# we iterate 2 extra times to demonstrate how 
# queue removes values from front
for i in range(7):
     # generate random numbers between 0 
    # to 100 (both inclusive)
    new_data_point = random.randint(0, 100)
    data_points.append(new_data_point)
    print(data_points)

輸出

deque([64], maxlen=5)
deque([64, 57], maxlen=5)
deque([64, 57, 15], maxlen=5)
deque([64, 57, 15, 31], maxlen=5)
deque([64, 57, 15, 31, 35], maxlen=5)
deque([57, 15, 31, 35, 25], maxlen=5)
deque([15, 31, 35, 25, 12], maxlen=5)

步驟3. 刪除第一個(gè)點(diǎn)

在Python中的動(dòng)態(tài)繪圖中,當(dāng)我們生成一個(gè)新的數(shù)據(jù)點(diǎn)并將其添加到固定長度的隊(duì)列中時(shí),我們需要從隊(duì)列中移除最舊的點(diǎn)以保持隊(duì)列的固定長度。在這里,我們按照FIFO原則從左側(cè)移除元素。

from collections import deque
import random
# Create a deque with fixed length
data_points = deque(maxlen=5)
# Append 5 data points to the deque at once using extend method.
data_points.extend([1, 2, 3, 4, 5])
# Print the deque before removing the first element
print("Deque before removing the first element:", data_points)
# Remove the first element from the deque
data_points.popleft()
# Print the deque after removing the first element
print("Deque after removing the first element:", data_points)

輸出

Deque before removing the first element: deque([1, 2, 3, 4, 5], maxlen=5)
Deque after removing the first element: deque([2, 3, 4, 5], maxlen=5)

步驟4. 繪制隊(duì)列,暫停繪圖以進(jìn)行可視化

我們首先使用Matplotlib繪制存儲(chǔ)在隊(duì)列中的數(shù)據(jù)點(diǎn),然后暫停繪制一定的時(shí)間,以便在使用下一組數(shù)據(jù)點(diǎn)更新之前可以可視化該圖。這可以使用Matplotlib中的plt.pause()函數(shù)來完成。在這里,在我們的示例代碼塊中,我們將生成一組y值,x軸值從0恒定增加,然后觀察圖,然后暫停它。

import matplotlib.pyplot as plt
from collections import deque
import random
# Create a fixed-length deque of size 50 to store the data points
data_points = deque(maxlen=50)
# Create an empty plot
fig, ax = plt.subplots()
line = ax.plot([])
# Set the x-axis and y-axis limits to 100
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
# Iterate through 50 data points and update the plot
for i in range(50):
    # Generate and add data points to the deque
    new_x = i
    # generate a random number between 0 to 100 for y axis
    new_y = random.randint(0, 100)
    data_points.append((new_x, new_y))
    # Update the plot with the new data points
    x_values = [x for x, y in data_points]
    y_values = [y for x, y in data_points]
    line.set_data(x_values, y_values)
    # pause the plot for 0.01s before next point is shown
    plt.pause(0.01)
# Show the plot
plt.show()

步驟5. 清除繪圖

實(shí)時(shí)更新數(shù)據(jù)點(diǎn)時(shí),我們將在繪制下一組值之前清除該圖。這可以使用Matplotlib中的line.set_data([],[])函數(shù)來完成,這樣我們就可以實(shí)時(shí)顯示隊(duì)列數(shù)據(jù)結(jié)構(gòu)的固定大小。

import matplotlib.pyplot as plt
from collections import deque
import random
# Create a fixed-length deque to store the data points
data_points = deque(maxlen=50)
# Create an empty plot
fig, ax = plt.subplots()
line, = ax.plot([])
# Set the x-axis and y-axis limits
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
# Iterate through the data points and update the plot
for i in range(100):
	# Generate and add data points to the deque
	new_x = i
	new_y = random.randint(0, 100)
	data_points.append((new_x, new_y))
	# Update the plot with the new data points
	x_values = [x for x, y in data_points]
	y_values = [y for x, y in data_points]
	line.set_data(x_values, y_values)
	plt.pause(0.01)
	# Clear the plot for the next set of values
	line.set_data([], [])
# Show the plot
plt.show()

動(dòng)態(tài)散點(diǎn)圖

import matplotlib.pyplot as plt
from collections import deque
import random
# Create a fixed-length deque of length 50 to store the data points
data_points = deque(maxlen=50)
# Create an empty plot
fig, ax = plt.subplots()
# Set the x-axis and y-axis limits to 100
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
# Create a scatter plot to visualize the data points
scatter = ax.scatter([], [])
# Iterate through the data points and update the scatter plot
for i in range(100):
    # Generate and add data points to the deque
    new_x = i
    new_y = random.randint(0, 100)
    data_points.append((new_x, new_y))
    # Update the scatter plot with the new data points
    x_values = [x for x, y in data_points]
    y_values = [y for x, y in data_points]
    scatter.set_offsets(list(zip(x_values, y_values)))
    plt.pause(0.01)
# Show the plot
plt.show()

動(dòng)態(tài)散點(diǎn)圖和折線圖

import matplotlib.pyplot as plt
from collections import deque
import random
from matplotlib.animation import FuncAnimation
# Create a fixed-length deque of length 50 to store the data points
data_points = deque(maxlen=50)
# Create an empty plot
fig, ax = plt.subplots()
line, = ax.plot([])
# Set the x-axis and y-axis limits to 100
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
# Create a scatter plot to visualize the data points
scatter = ax.scatter([], [])
# Iterate through the data points and update the scatter plot
for i in range(100):
	# Generate and add data points to the deque
	new_x = i
	new_y = random.randint(0, 100)
	data_points.append((new_x, new_y))
	# Update the scatter plot with the new data points
	x_values = [x for x, y in data_points]
	y_values = [y for x, y in data_points]
	scatter.set_offsets(list(zip(x_values, y_values)))
	line.set_data(x_values, y_values)
	plt.pause(0.01)
# Save the animation as an animated GIF
plt.show()

以上就是Python利用matplotlib實(shí)現(xiàn)動(dòng)態(tài)可視化詳解的詳細(xì)內(nèi)容,更多關(guān)于matplotlib動(dòng)態(tài)可視化的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python使用protobuf序列化和反序列化的實(shí)現(xiàn)

    Python使用protobuf序列化和反序列化的實(shí)現(xiàn)

    protobuf是一種二進(jìn)制的序列化格式,相對于json來說體積更小,傳輸更快,本文主要介紹了Python使用protobuf序列化和反序列化的實(shí)現(xiàn),感興趣的可以了解一下
    2021-05-05
  • 使用tensorflow實(shí)現(xiàn)矩陣分解方式

    使用tensorflow實(shí)現(xiàn)矩陣分解方式

    今天小編就為大家分享一篇使用tensorflow實(shí)現(xiàn)矩陣分解方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python按照list中字典的某key去重的示例代碼

    python按照list中字典的某key去重的示例代碼

    這篇文章主要介紹了python按照list中字典的某key去重的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • python PyQt5中單行文本輸入控件QLineEdit用法詳解

    python PyQt5中單行文本輸入控件QLineEdit用法詳解

    在PyQt5的GUI編程中,QLineEdit控件是一個(gè)用于輸入和編輯單行文本的部件,它提供了豐富的功能和靈活性,可以輕松地實(shí)現(xiàn)用戶輸入的捕獲、驗(yàn)證和格式化等功能,本文將通過實(shí)際案例詳細(xì)介紹QLineEdit控件的常用方法,需要的朋友可以參考下
    2024-08-08
  • python列表的特點(diǎn)分析

    python列表的特點(diǎn)分析

    在本篇文章里小編個(gè)大家整理的是一篇關(guān)于python列表的特點(diǎn)分析內(nèi)容總結(jié),有需要的朋友們可以學(xué)習(xí)下。
    2021-08-08
  • pycharm出現(xiàn)No?pyvenv.cfg?file錯(cuò)誤的問題解決

    pycharm出現(xiàn)No?pyvenv.cfg?file錯(cuò)誤的問題解決

    本文主要介紹了pycharm出現(xiàn)No?pyvenv.cfg?file錯(cuò)誤的問題解決,主要是通過恢復(fù)歷史記錄中的未刪除狀態(tài)來解決,下面就來詳細(xì)的介紹一下,感興趣的可以了解一下
    2025-05-05
  • Python利用pathlib進(jìn)行路徑操作的最佳實(shí)踐

    Python利用pathlib進(jìn)行路徑操作的最佳實(shí)踐

    在Python文件操作中,路徑處理是高頻需求,本文通過實(shí)際案例對比,揭示pathlib如何成為現(xiàn)代Python開發(fā)者的首選工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2026-01-01
  • Python實(shí)現(xiàn)整數(shù)與浮點(diǎn)數(shù)相互轉(zhuǎn)換的方法

    Python實(shí)現(xiàn)整數(shù)與浮點(diǎn)數(shù)相互轉(zhuǎn)換的方法

    在Python編程的浩瀚宇宙中,數(shù)字類型是最基礎(chǔ)卻最強(qiáng)大的基石之一,整數(shù)(int)和浮點(diǎn)數(shù)(float)作為日常開發(fā)中不可或缺的兩種數(shù)據(jù)類型,它們之間的相互轉(zhuǎn)換看似簡單,卻暗藏玄機(jī),本文將帶你深入探索Python中整數(shù)與浮點(diǎn)數(shù)相互轉(zhuǎn)換的完整知識體系,需要的朋友可以參考下
    2026-04-04
  • Python安裝、卸載及環(huán)境配置全指南(解決常見問題與報(bào)錯(cuò))

    Python安裝、卸載及環(huán)境配置全指南(解決常見問題與報(bào)錯(cuò))

    Python作為當(dāng)今最流行的編程語言之一,廣泛應(yīng)用于數(shù)據(jù)分析、人工智能、Web開發(fā)等領(lǐng)域,然而,許多用戶在安裝、卸載Python或配置環(huán)境時(shí),經(jīng)常會(huì)遇到各種問題,本文將從?Python安裝、環(huán)境變量配置、卸載修復(fù)、虛擬環(huán)境管理?等方面,提供完整的解決方案,需要的朋友可以參考下
    2025-05-05
  • Python對Tornado請求與響應(yīng)的數(shù)據(jù)處理

    Python對Tornado請求與響應(yīng)的數(shù)據(jù)處理

    這篇文章主要介紹了Python對Tornado請求與響應(yīng)的數(shù)據(jù)處理,需要的朋友可以參考下
    2020-02-02

最新評論

正阳县| 上虞市| 会东县| 南宁市| 安康市| 满城县| 海城市| 上虞市| 溆浦县| 宣恩县| 庄浪县| 沾益县| 天气| 无棣县| 枞阳县| 乌恰县| 靖远县| 四川省| 高唐县| 泊头市| 台北市| 垫江县| 淳化县| 巨野县| 正蓝旗| 黔江区| 淄博市| 新竹县| 平原县| 甘孜| 滕州市| 新乐市| 永寿县| 麻阳| 开化县| 绥芬河市| 高密市| 来安县| 张家川| 尉氏县| 调兵山市|