使用matplotlib实现动态可视化

Python中的数据可视化是指原始数据的图形表示,以更好地可视化、理解和推理。Python提供了各种库,包含用于可视化数据的不同特性,并且可以支持不同类型的图形,即Matplotlib、Seaborn、Bokeh、Plotly等。

Python中的动态可视化

任何系统的动态可视化都意味着在演示过程中以图形方式表示当前系统的状态变化。在Python中的数据可视化方面,动态可视化是一个动态图形,它要么随着时间的推移而变化,就像在视频中一样,要么随着用户改变输入而变化,但在当前演示中,它就像是活的一样。

在Python中创建动态图的步骤

步骤1. 创建固定长度的队列

队列是一种线性数据结构,以先进先出(FIFO)原则存储项目。它可以在Python中以各种方式实现。在Python中创建一个固定长度的队列用于动态绘图是指创建一个数据结构,该结构可以存储固定数量的元素,并在容器满时丢弃最旧的元素。当我们要绘制不断更新的数据点时,它非常有用。通过限制容器的大小,可以提高绘制的性能和清晰度。

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. 生成点并将其保存到队列

在这里,我们动态地生成数据点,并将它们附加到队列数据结构中,而不是像上面的示例中所示的那样执行手动操作。这里我们将使用Python的random模块中可用的函数来生成数据点。

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中的动态绘图中,当我们生成一个新的数据点并将其添加到固定长度的队列中时,我们需要从队列中移除最旧的点以保持队列的固定长度。在这里,我们按照FIFO原则从左侧移除元素。

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绘制存储在队列中的数据点,然后暂停绘制一定的时间,以便在使用下一组数据点更新之前可以可视化该图。这可以使用Matplotlib中的plt.pause()函数来完成。
在这里,在我们的示例代码块中,我们将生成一组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()

使用matplotlib实现动态可视化_第1张图片

步骤5. 清除绘图

实时更新数据点时,我们将在绘制下一组值之前清除该图。这可以使用Matplotlib中的line.set_data([],[])函数来完成,这样我们就可以实时显示队列数据结构的固定大小。

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

动态散点图

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

使用matplotlib实现动态可视化_第2张图片

动态散点图和折线图

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

使用matplotlib实现动态可视化_第3张图片

你可能感兴趣的:(matplotlib)