三种Matplotlib中动态更新绘图的方法总结

 更新时间:2024年04月09日 08:38:25   作者:python收藏家  
这篇文章主要为大家详细介绍了如何随着数据的变化动态更新Matplotlib(Python的数据可视化库)图,文中介绍了常用的三种方法,希望对大家有所帮助

本文展示了如何随着数据的变化动态更新Matplotlib(Python的数据可视化库)图。它提供了两种绘图方法-第一种是API(适用于大型程序或需要深度控制的程序),第二种是Pyplot接口(受Matlab启发)。在本文中,我们将展示如何在Pyplot环境中动态更新图。

使用Matplotlib Pyplot绘制线图

在创建一个动态更新的图之前,让我们首先使用Matplotlib创建/绘制一个简单的静态线图。此图稍后将升级为动态更新数据。下面是一个使用Matplotlib创建静态线图的程序。

import matplotlib.pyplot as plt

x = [1,2,3,4] # x-coordinates of the data points
y = [4,7,6,8] # y-coordinates of the data points

graph = plt.plot(x,y) # plotting the data and storing the graph in variable named graph
plt.show()			 # showing the resultant graph

在Matplotlib中动态更新绘图

1.使用matplotlib.animations

我们可以使用“matplotlib.animations.FuncAnimation”函数来更新绘图。

from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import random

# initial data
x = [1]
y = [random.randint(1,10)]

# creating the first plot and frame
fig, ax = plt.subplots()
graph = ax.plot(x,y,color = 'g')[0]
plt.ylim(0,10)


# updates the data and graph
def update(frame):
	global graph

	# updating the data
	x.append(x[-1] + 1)
	y.append(random.randint(1,10))

	# creating a new graph or updating the graph
	graph.set_xdata(x)
	graph.set_ydata(y)
	plt.xlim(x[0], x[-1])

anim = FuncAnimation(fig, update, frames = None)
plt.show()

2.使用pyplot交互模式更新Matplotlib图

默认情况下,交互模式是关闭的,因此只有在调用show函数时才会绘制绘图。此外,在show函数处停止执行,直到图形关闭。然而,我们可以通过调用函数.ion()来打开交互模式。当交互模式打开时,图形会立即绘制,并在我们对其进行任何更改时立即更新。我们可以使用此行为使用以下方法动态更新绘图

import matplotlib.pyplot as plt
import random

plt.ion() # turning interactive mode on

# preparing the data
y = [random.randint(1,10) for i in range(20)]
x = [*range(1,21)]

# plotting the first frame
graph = plt.plot(x,y)[0]
plt.ylim(0,10)
plt.pause(1)

# the update loop
while(True):
	# updating the data
	y.append(random.randint(1,10))
	x.append(x[-1]+1)
	
	# removing the older graph
	graph.remove()
	
	# plotting newer graph
	graph = plt.plot(x,y,color = 'g')[0]
	plt.xlim(x[0], x[-1])
	
	# calling pause function for 0.25 seconds
	plt.pause(0.25)

3.Matplotlib更新散点图的示例

在这个例子中,我们使用“Figure.canvas.draw()”函数更新matplotlib散点图。

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random

# initial data
x = [random.randint(1,100)]
y = [random.randint(1,100)]

# creating the figure and axes object
fig, ax = plt.subplots()

# update function to update data and plot
def update(frame):
	# updating the data by adding one more point
	x.append(random.randint(1,100))
	y.append(random.randint(1,100))

	ax.clear() # clearing the axes
	ax.scatter(x,y, s = y, c = 'b', alpha = 0.5) # creating new scatter chart with updated data
	fig.canvas.draw() # forcing the artist to redraw itself

anim = FuncAnimation(fig, update)
plt.show()

总结

至少有3种方法可以在matplotlib中完成动态更新绘图的任务。首先使用matplotlib.animations的FuncAnimation函数,其中定义了更新函数,该函数在每帧更新数据和图形,其次使用matplotlib交互模式,该模式通过创建更新数据的更新循环来利用图像在交互模式中即时更新的事实,并在每个周期更新图形,最后使用“figure.canvas.draw()”方法在每次更新后强制当前轴的更新后重新绘制图形。

到此这篇关于三种Matplotlib中动态更新绘图的方法总结的文章就介绍到这了,更多相关Matplotlib动态绘图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python中文字符串截取问题

    Python中文字符串截取问题

    web应用难免会截取字符串的需求,Python中截取英文很容易,但是截取utf-8的中文机会截取一半导致一些不是乱码的乱码.其实utf8截取很简单,这里记下来分享给大家
    2015-06-06
  • pybind11在Windows下的使用教程

    pybind11在Windows下的使用教程

    Pybind11算是目前最方便的Python调用C++的工具了, 介绍一下在vs2019上写Python的扩展的HelloWorld,感兴趣的朋友跟随小编一起看看吧
    2019-07-07
  • Python中不可错过的五个超有用函数

    Python中不可错过的五个超有用函数

    在本文中,我们用代码详细说明了Python中超实用的5个函数的重要作用,这些函数虽然简单,但却是Python中功能最强大的函数,下面一起来看看文章的详细介绍吧,希望对你的学习有所帮助
    2022-01-01
  • 使用Python批量合并多个Excel文件的代码实现

    使用Python批量合并多个Excel文件的代码实现

    本文介绍如何使用Python批量合并多个Excel文件,通过定义函数,可以轻松地将指定文件夹内的所有Excel文件整合到一个文件中,并提供了按文件名提取日期信息及按原文件名区分Sheet的方法,需要的朋友可以参考下
    2025-12-12
  • Python入门_浅谈逻辑判断与运算符

    Python入门_浅谈逻辑判断与运算符

    下面小编就为大家带来一篇Python入门_浅谈逻辑判断与运算符。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Python操作Elasticsearch处理timeout超时

    Python操作Elasticsearch处理timeout超时

    这篇文章主要介绍了Python操作Elasticsearch处理timeout超时,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Python中模块与包有相同名字的处理方法

    Python中模块与包有相同名字的处理方法

    这篇文章主要给大家介绍了在Python中模块与包有相同名字的处理方法,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-05-05
  • Pycharm中配置远程Docker运行环境的教程图解

    Pycharm中配置远程Docker运行环境的教程图解

    这篇文章主要介绍了Pycharm中配置远程Docker运行环境,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • Python实现排序算法、查找算法和图遍历算法实例

    Python实现排序算法、查找算法和图遍历算法实例

    这篇文章主要介绍了Python实现排序算法、查找算法和图遍历算法实例,排序算法、查找算法和图遍历算法是计算机科学中常见且重要的算法。它们在数据处理、搜索和图结构等领域发挥着关键作用,需要的朋友可以参考下
    2023-08-08
  • 如何基于python实现单目三维重建详解

    如何基于python实现单目三维重建详解

    单目三维重建是根据单个摄像头的运动模拟双目视觉获得物体在空间中的三维视觉信息,下面这篇文章主要给大家介绍了关于如何基于python实现单目三维重建的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06

最新评论