Python利用matplotlib实现动态可视化详解

 更新时间:2023年08月28日 14:56:27   作者:python收藏家  
Python中的数据可视化是指原始数据的图形表示,以更好地可视化、理解和推理,Python提供了各种库,包含用于可视化数据的不同特性,下面我们就来看看如何利用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()

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

动态散点图和折线图

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实现动态可视化详解的详细内容,更多关于matplotlib动态可视化的资料请关注脚本之家其它相关文章!

相关文章

  • Python列表的深复制和浅复制示例详解

    Python列表的深复制和浅复制示例详解

    这篇文章主要给大家介绍了关于Python列表的深复制和浅复制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python动态规划之零钱兑换问题详解

    Python动态规划之零钱兑换问题详解

    这篇文章主要介绍了Python动态规划之零钱兑换问题详解,这次我们就按照套路模板,再来剖析一道经典动规题目零钱兑换,计算并返回可以凑成总金额所需的 最少的硬币个数 如果没有任何一种硬币组合能组成总金额,返回-1,需要的朋友可以参考下
    2023-11-11
  • Django自定义分页与bootstrap分页结合

    Django自定义分页与bootstrap分页结合

    这篇文章主要为大家详细介绍了Django自定义分页与bootstrap分页结合使用的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Python操作PostgreSQL数据库的基本方法(增删改查)

    Python操作PostgreSQL数据库的基本方法(增删改查)

    PostgreSQL数据库是最常用的关系型数据库之一,最吸引人的一点是它作为开源数据库且具有可拓展性,能够提供丰富的应用,这篇文章主要给大家介绍了关于Python操作PostgreSQL数据库的基本方法,文中介绍了连接PostgreSQL数据库,以及增删改查,需要的朋友可以参考下
    2023-09-09
  • Pytorch相关知识介绍与应用

    Pytorch相关知识介绍与应用

    最近又重拾了机器学习的相关技术,在本科毕设的阶段下学习使用了Tensorflow 2.x工具,当时也是不求甚解,直接拿来用了,但现在已经有充足的时间、精力和基础知识来重新学习一下
    2022-11-11
  • Python编程实现线性回归和批量梯度下降法代码实例

    Python编程实现线性回归和批量梯度下降法代码实例

    这篇文章主要介绍了Python编程实现线性回归和批量梯度下降法代码实例,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • HTTPX入门使用教程

    HTTPX入门使用教程

    HTTPX是一款Python栈HTTP客户端库,它提供了比标准库更高级别、更先进的功能,如连接重用、连接池、超时控制、自动繁衍请求,下面通过本文介绍HTTPX入门知识和基本用法,感兴趣的朋友一起看看吧
    2023-12-12
  • 利用scrapy将爬到的数据保存到mysql(防止重复)

    利用scrapy将爬到的数据保存到mysql(防止重复)

    这篇文章主要给大家介绍了关于利用scrapy将爬到的数据保存到mysql(防止重复)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2018-03-03
  • 基于Python编写一个DOS命令辅助工具

    基于Python编写一个DOS命令辅助工具

    在日常系统管理和维护工作中,执行DOS(Disk Operating System)命令是一项必不可少的任务,下面我们就来看看如何使用Python编写一个简单的DOS命令辅助工具,简化系统管理任务吧
    2024-01-01
  • python实现的一个火车票转让信息采集器

    python实现的一个火车票转让信息采集器

    这篇文章主要介绍了python实现的一个火车票转让信息采集器,采集信息来源是58同程或者赶集网,需要的朋友可以参考下
    2014-07-07

最新评论