如何使用Celluoid制作可视化动画
Celluoid可以简化matplotlib生成可视化动画的代码量,Celluoid会先生成一个画布,然后每隔一段时间对绘图进行拍照(帧),最后将所有的帧合并。
今天讲三个例子
单图
多图
图例
!pip3 install celluloid
from matplotlib import pyplot as plt
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
for i in range(10):
plt.plot([i] * 10)
camera.snap()
animation = camera.animate()
animation.save('celluloid_minimal.gif', writer = 'imagemagick')
动图中有显示多个子图
import numpy as np
from matplotlib import pyplot as plt
from celluloid import Camera
fig, axes = plt.subplots(2)
camera = Camera(fig)
t = np.linspace(0, 2 * np.pi, 128, endpoint=False)
for i in t:
axes[0].plot(t, np.sin(t + i), color='blue')
axes[1].plot(t, np.sin(t - i), color='blue')
camera.snap()
animation = camera.animate()
animation.save('celluloid_subplots.gif', writer = 'imagemagick')
import matplotlib
from matplotlib import pyplot as plt
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
for i in range(20):
t = plt.plot(range(i, i + 5))
plt.legend(t, [f'line {i}'])
camera.snap()
animation = camera.animate()
animation.save('celluloid_legends.gif', writer = 'imagemagick')
jupyter notebook代码获取方式,公众号后台回复关键词“20200229”