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

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

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

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

隊列是一種線性數(shù)據(jù)結(jié)構(gòu),以先進(jìn)先出(FIFO)原則存儲項目。它可以在Python中以各種方式實現(xiàn)。在Python中創(chuàng)建一個固定長度的隊列用于動態(tài)繪圖是指創(chuàng)建一個數(shù)據(jù)結(jié)構(gòu),該結(jié)構(gòu)可以存儲固定數(shù)量的元素,并在容器滿時丟棄最舊的元素。當(dāng)我們要繪制不斷更新的數(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)繪圖中,當(dāng)我們生成一個新的數(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. 繪制隊列,暫停繪圖以進(jìn)行可視化

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

相關(guān)文章

最新評論