Python matplotlib 绘制动态图 复习回顾 1

这是我参与11月更文挑战的第20天,活动详情查看:2021最后一次更文挑战

复习回顾

在matplotlib模块中我们前面学习绘制如折线、柱状、散点、直方图等静态图形。我们都知道在matplotlib模块主要有三层脚本层为用户提供快捷的绘制图形方法,美工层接收到脚本层的命令后将绘制指令发送给后端,后端提供执行绘制操作、事件响应、图形渲染工作。具体的详情可见往期文章如下

在matplotlib模块中,除了以上静态图形的绘制,还提供Animation类支持绘制动态图制作

image.png

本期,我们对matplotlib.animation绘制动态图方法学习,Let’s go~

  1. Animation 概述

Animation 是matplotlib模块制作实时动画的动画类,包含三个子类

  • Animation 是动画类的基类
  • TimedAnimation 是 Animation的子类,可通过绘制时间绘制每一帧动画
  • FuncAnimation 是基于Timed子类,可以通过重复调用fun()方法来绘制动画
  • ArtistAnimation 使用一组Artist对象来绘制动画

image.png

  • 绘制动画特点

+ 绘制对象引用:动画对象要在制作动画时要保持长期有效,否则会被系统资源回收,动画暂停
+ 动画计时器:是对动画对象推进的唯一引用对象
+ 动画保存:需要使用animation.save、animation.To\_html5\_video或animation.To\_jshtml进行动画保存
+ matpoltlib.animation 还提供关于电影格式的类
  • 动画制作方法

matplotlib.animation.Animation()是动画类的基类,是不能被使用的。常用的两个类主要animation两个子类

+ matplotlib.animation.FuncAnimation

1
2
3
4
5
6
7
Python复制代码matplotlib.animation.FuncAnimation(fig, func, 
frames=None,
init_func=None, 
fargs=None, 
save_count=None, 
* , cache_frame_data=True, 
**kwargs)
+ matplotlib.animation.ArtistAnimation
1
2
3
4
python复制代码matplotlib.animation.ArtistAnimation(fig, 
artists, 
*args, 
**kwargs)
  1. 绘制动态图步骤

matplotlib 绘制动态图最重要的是要准备好每一帧显示的数据,通常我们使用FuncAnimation可以传入产生连续数字的func方法,因此绘制动态图主要步骤为:

  • 导入绘制图形的matplotlib.pyplot和制作动态图的matplotlib.animation
1
2
python复制代码import matplotlib.pyplot as plt
import matplotlib.animation as animation
  • 使用Pyplot.subplots创建一个fig画布对象和一组子图
1
python复制代码fig,ax = plt.subplots()
  • 调用numpy.random或者numpy.arange()等方法准备x,y轴数据
1
python复制代码x = np.arange(0, 2*np.pi, 0.01)
  • Axes对象调用plot()、scatter()、hist()等绘制方法,并赋值给list对象
1
python复制代码line, = ax.plot(x, np.cos(x),color="pink")
  • 需要定义一个专门update data方法生成每一帧显示的数据例如func()
1
2
3
python复制代码def update(i):
line.set_ydata(np.cos(x + i / 50))
return line,
  • 调用animation.FuncAnimation把fig和update()方法
1
2
python复制代码ani = animation.FuncAnimation(
fig, update, interval=20, blit=True, save_count=50)
  • 调用plt.show()显示出动态图
1
python复制代码plt.show()
  • 我们可以调用animation.save(“movie.gif”,writer=”pillow”)保存动画为gif格式

ps:我们需要提前pip install pillow 安装pillow库,否则会提示无法使用

1
2
python复制代码
ani.save("movie.gif",writer='pillow')

movie.gif

  1. 小试牛刀

我们使用animation类绘制直方动态图,在绘制的过程中需要注意几点

  • 使用numpy.linspace生成100个在-5,5的等差数列
  • 使用numpy.random.randn()生成随机数据
  • Axes对象调用hist()返回n,bins,BarContainer
  • 定义一个递归update()函数,使用Python闭包跟踪barcontainer来更新每次直方图矩形高度
  • 调用animation.FuncAnimation()方法绘制动态图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
python复制代码def drawanimationhist():
fig, ax = plt.subplots()
BINS = np.linspace(-5, 5, 100)
data = np.random.randn(1000)
n, _ = np.histogram(data, BINS)
_, _, bar_container = ax.hist(data, BINS, lw=2,
ec="b", fc="pink")
def update(bar_container):
def animate(frame_number):
data = np.random.randn(1000)
n, _ = np.histogram(data, BINS)
for count, rect in zip(n, bar_container.patches):
rect.set_height(count)
return bar_container.patches
return animate

ax.set_ylim(top=55)

ani = animation.FuncAnimation(fig, update(bar_container), 50,
repeat=False, blit=True)
plt.show()

hist.gif

总结

本期,我们对matplotlib模块制作动态图类animation相关方法学习。在绘制动态图过程中,需要定义func方法来更新每一帧所需要的数据。

以上是本期内容,欢迎大佬们点赞评论,下期见~

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%