脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 如何利用Matplotlib库绘制动画及保存GIF图片

如何利用Matplotlib库绘制动画及保存GIF图片

2021-11-24 11:07frank909 Python

这篇文章主要给大家介绍了关于如何利用Matplotlib库绘制动画及保存GIF图片的相关资料,matplotlib模块提供了很高级和非常友好的使用方式,使用起来也是非常方便的,需要的朋友可以参考下

前言

在自学机器学习或者是深度学习的过程中,有的时候总想把执行过程或者执行结果显示出来,所以就想到了动画。好在用 Python 实现动画有许多中方式,而大家熟知的 Matplotlib 库就可以实现。

本文的目的是对 Matplotlib 的动画实现手段做一个简单的说明。

绘制动画

?
1
2
import matplotlib.pyplot as plt
import matplotlib.animation as animation

如果要让 matplotlib 实现动画功能的话,那么就要引入 animation 模块。

然后再创建 animation 的对象。

?
1
2
anim = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
                              repeat=False, init_func=init)

animation 的实现类是 FuncAnimation,它有一个构造方法。下面先通过一个示例,讲解 animation 的基本用法,然后再来细致分析 FuncAnimation 构造方法中各项参数的意义。

我们的目标是做一个 Sin 函数的动画示例。

代码很简单。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
 
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro',animated=True)
 
def init():
    ax.set_xlim(-np.pi,np.pi)
    ax.set_ylim(-1, 1)
    return ln,
 
def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return ln,
 
anim = animation.FuncAnimation(fig, update, frames=np.linspace(-np.pi,np.pi, 90),interval=10,
                    init_func=init,blit=True)
plt.show()

核心代码是这一行。

?
1
2
anim = animation.FuncAnimation(fig, update, frames=np.linspace(-np.pi,np.pi, 90),interval=10,
                    init_func=init,blit=True)

按照上面的示例代码,我们可以依葫芦画瓢编写动画代码了。

如何利用Matplotlib库绘制动画及保存GIF图片

但,如果我们需要达到灵活运用的话,就需要花点心思,了解它们的机制。

我们先来看看 FuncAnimation 的构造方法。

?
1
2
def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
                 save_count=None, **kwargs):

fig 自然是 matplotlib 中的 figure 对象。

func 是每一次更新时所调用的方法,它是回调函数。因此,我们可以在这个方法中更新 figure 当中的 axes 中的 line2d 对象,它是动态更新 figure 的根本。

frames 代表了整个动画过程中帧的取值范围,而本质上是一个数据发生器。我将在后面重点讲解它。

init_func 是初始函数,用来初始 figure 的画面。

fargs 是每次附加给 func 回调函数的参数,可以为 None

save_count 是缓存的数量

除此之外,还有一些可选的参数,它们分别是

interval 是每 2 个 frame 发生的时间间隔,单位是 ms,默认值是 200.

repeat_delay 取值是数值,如果 animation 是重复播放的话,这个值就是每次播放之间的延迟时间,单位是 ms。

repeat bool 型可选参数,默认为 True,代表动画是否会重复执行

blit bool 型可选参数,控制绘制的优化。默认是 False。

如何理解 animation 呢?

我认为,animation 的核心是 frames 和 func。

frames 可以取值:iterable,int,generator 生成器函数 或者是 None。

在上面的代码中,我们给 frames 的取值是这样的。

?
1
frames=np.linspace(-np.pi,np.pi, 90)

其实就是一个 list,它的值范围为 -pi 到 pi,frames 总共有 90 帧,而 list 是一个 iterable 类型,所以它可以不停的迭代。

frames 也可以取值为整数,相当于给参数赋值 range(frames)。

frames 也可以取值为 None,那么它的结果相当于传递 itertools.count,结构就是从 0 开始,每次步进 1,无限的执行下去。

frames 还接受 generator 函数,也就是生成器,但有个前提是,生成器要符合下面的签名格式。

?
1
def gen_function() -> obj

参数列表为空,但需要返回一个值,这个值就会传入到 func 回调函数当中。

func 是回调函数,它会在每次更新的时候被调用,所以我们只需要在这个函数中更新 figure 中的数值就可以了,就像下面代码。

?
1
2
3
4
5
def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return ln,

frames 和 func 的关系是什么?

实际上,frames 决定了整个动画 frame 的取值范围,它会在 interval 时间内迭代一次,然后将值传递给 func,直到整个 frames 迭代完毕。

我本人而言,也更倾向于用 generator 函数去定义 frames 而不是直接分配一个列表,所以我可以将之前的代码改写如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
 
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro',animated=True)
 
def init():
    ax.set_xlim(-np.pi,np.pi)
    ax.set_ylim(-1, 1)
    return ln,
 
def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return ln,
 
def data_gen():
 
    frame = -np.pi
 
    step = 2 * np.pi / 90
 
    while frame < np.pi:
        frame += step
        yield frame
 
# anim = animation.FuncAnimation(fig, update, frames=np.linspace(-np.pi,np.pi, 360),interval=10,
#                     init_func=init,blit=True)
anim = animation.FuncAnimation(fig, update, frames=data_gen,interval=10,
                    init_func=init,blit=True)
 
plt.show()

data_gen 就是一个生成器函数,它会每隔 10ms 运行一次,然后将结果传递给 update 函数。

data_gen 里面运用到了 yield 关键字,这是的我们可以在每次迭代时才返回相应的结构,而不要在一开始就分配。如果不熟悉这方面知识点的同学,可以自行搜索相应的知识。

保存动画

因为经常写博客,所以也经常需要将结果保存下来,一般我会保存为 .gif 格式图片,本篇博文的 gif 图像就是通过 matplotlib 保存的。

如何利用Matplotlib库绘制动画及保存GIF图片

好在用 matplotlib 实现它也并不难。

?
1
anim.save('test_animation.gif',writer='imagemagick')

一句代码就搞定了,运行成功后,会在当前目录下生成 test_animation.gif 图像。

需要注意到的是,如果要保存 gif 图像,这要求开发者电脑已经安装了 ImageMagicK。

ubuntu 用户可以通过如下命令安装。

?
1
sudo apt-get install imagemagick

并且,动画保存的时候要指定 writer 为 imagemagick.

动画可以保存为 gif 图像,自然也能保存为 mp4 视频格式。

但这要求开发者计算机已经安装好 ffmpeg 库,并且 save 方法中指定 writer 为 ffmpeg,具体细节请读者自行扩展阅读。

总结

到此这篇关于如何利用Matplotlib库绘制动画及保存GIF图片的文章就介绍到这了,更多相关Matplotlib库绘制动画内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://frank909.blog.csdn.net/article/details/84940997

延伸 · 阅读

精彩推荐