分享

如何使用Celluoid制作可视化动画

 大邓的Python 2021-02-23

Celluoid可以简化matplotlib生成可视化动画的代码量,Celluoid会先生成一个画布,然后每隔一段时间对绘图进行拍照(帧),最后将所有的帧合并。

今天讲三个例子

  1. 单图

  2. 多图

  3. 图例

安装





  1. !pip3 install celluloid

单图





  1. from matplotlib import pyplot as plt

  2. from celluloid import Camera

  3. fig = plt.figure()

  4. camera = Camera(fig)

  5. for i in range(10):

  6. plt.plot([i] * 10)

  7. camera.snap()

  8. animation = camera.animate()

  9. animation.save('celluloid_minimal.gif', writer = 'imagemagick')

多图





动图中有显示多个子图

  1. import numpy as np

  2. from matplotlib import pyplot as plt

  3. from celluloid import Camera

  4. fig, axes = plt.subplots(2)

  5. camera = Camera(fig)

  6. t = np.linspace(0, 2 * np.pi, 128, endpoint=False)

  7. for i in t:

  8. axes[0].plot(t, np.sin(t + i), color='blue')

  9. axes[1].plot(t, np.sin(t - i), color='blue')

  10. camera.snap()

  11. animation = camera.animate()

  12. animation.save('celluloid_subplots.gif', writer = 'imagemagick')

显示图例





  1. import matplotlib

  2. from matplotlib import pyplot as plt

  3. from celluloid import Camera

  4. fig = plt.figure()

  5. camera = Camera(fig)

  6. for i in range(20):

  7. t = plt.plot(range(i, i + 5))

  8. plt.legend(t, [f'line {i}'])

  9. camera.snap()

  10. animation = camera.animate()

  11. animation.save('celluloid_legends.gif', writer = 'imagemagick')

近期文章





《用Pandas做数据分析》

《Python网络爬虫与文本数据分析》

漂亮~pandas可以无缝衔接Bokeh

如何使用Matplotlib制作出动画??

代码不到40行的超燃动态排序图

Lazy Prices公司年报内容变动碰上股价偷懒

用python帮你生产指定内容的word文档

2020年B站跨年晚会弹幕内容分析

YelpDaset: 酒店管理类数据集10+G

NRC词语情绪词典和词语色彩词典

Loughran&McDonald金融文本情感分析库

股评师分析报告文本情感分析预测股价

使用分析师报告中含有的情感信息预测上市公司股价变动

综述:文本分析在市场营销研究中的应用

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多