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

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

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

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

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

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

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

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

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

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. 生成點并將其保存到隊列

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

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. 刪除第一個點

在Python中的動態(tài)繪圖中,當我們生成一個新的數(shù)據(jù)點并將其添加到固定長度的隊列中時,我們需要從隊列中移除最舊的點以保持隊列的固定長度。在這里,我們按照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. 繪制隊列,暫停繪圖以進行可視化

我們首先使用Matplotlib繪制存儲在隊列中的數(shù)據(jù)點,然后暫停繪制一定的時間,以便在使用下一組數(shù)據(jù)點更新之前可以可視化該圖。這可以使用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ù)據(jù)點時,我們將在繪制下一組值之前清除該圖。這可以使用Matplotlib中的line.set_data([],[])函數(shù)來完成,這樣我們就可以實時顯示隊列數(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()

動態(tài)散點圖

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()

動態(tài)散點圖和折線圖

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實現(xiàn)動態(tài)可視化詳解的詳細內(nèi)容,更多關(guān)于matplotlib動態(tài)可視化的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

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

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

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

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

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

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

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

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

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

    python列表的特點分析

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

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

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

    Python利用pathlib進行路徑操作的最佳實踐

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

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

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

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

    Python作為當今最流行的編程語言之一,廣泛應(yīng)用于數(shù)據(jù)分析、人工智能、Web開發(fā)等領(lǐng)域,然而,許多用戶在安裝、卸載Python或配置環(huán)境時,經(jīng)常會遇到各種問題,本文將從?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

最新評論

健康| 调兵山市| 德格县| 夏津县| 巴塘县| 阜新市| 平昌县| 高阳县| 嘉峪关市| 靖江市| 伊金霍洛旗| 弥渡县| 安泽县| 涟水县| 蓝田县| 武义县| 庆安县| 清新县| 安徽省| 林西县| 施秉县| 石门县| 恩平市| 澄江县| 海门市| 奎屯市| 黄石市| 麟游县| 镶黄旗| 望江县| 抚远县| 康平县| 成都市| 白水县| 偃师市| 西平县| 驻马店市| 徐闻县| 轮台县| 皮山县| 德阳市|