使用numpngw和matplotlib生成png动画的示例代码

作者:mighty13 时间:2023-06-15 13:38:21 

在matplotlib官网看到了第三方库numpngw的简介,利用该库作为插件可以辅助matplotlib生成png动画。

numpngw概述

numpngw库可生成PNG静态图像和PNG动画。

  • 通过write_png函数可以将 numpy保存为PNG 文件。

  • 通过 write_apng 函数可以将数组序列保存为 PNG 动画(APNG)文件 。

  • 通过AnimatedPNGWriter类可以将Matplotlib 保存为PNG动画文件。

numpngw库的依赖包是numpy和setuptools。

使用numpngw和matplotlib生成png动画

numpngw+matplotlib实现png动画


import numpy as np
from matplotlib import pyplot as plt
import matplotlib.animation as animation
from numpngw import AnimatedPNGWriter

t = np.linspace(0, 6, 100)
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
data=[i for i in zip(x,y)]

def plot_love(data):
 x, y = data
 plt.scatter(x, y, 60, c="r", alpha=0.7, marker=r"$\heartsuit$")
fig=plt.figure(figsize=(5, 3), dpi=100)
plt.axis("off")

writer = AnimatedPNGWriter(fps=12)
animator = animation.FuncAnimation(fig, plot_love, frames=data)
animator.save("love.png", writer=writer)

使用matplotlib和pillow实现gif动画


from matplotlib import pyplot as plt
import matplotlib.animation as animation
import numpy as np

t = np.linspace(0, 6, 100)
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
data=[i for i in zip(x,y)]

def plot_love(data):
 x, y = data
 plt.scatter(x, y, 60, c="r", alpha=0.7, marker=r"$\heartsuit$")

fig=plt.figure(figsize=(5, 3), dpi=100)
plt.axis("off")
animator = animation.FuncAnimation(fig, plot_love, frames=data, interval=80)
animator.save("love.gif", writer='pillow')

关键代码解读


# 导入AnimatedPNGWriter
from numpngw import AnimatedPNGWriter

# 初始化AnimatedPNGWriter
writer = AnimatedPNGWriter(fps=12)
# 将save函数中的writer参数设为AnimatedPNGWriter实例
animator.save("love.png", writer=writer)

通过对比可知,使用 numpngw+matplotlib生成png动画方式非常简单,只用初始化AnimatedPNGWriter,在save函数中指定writer即可。

来源:https://blog.csdn.net/mighty13/article/details/111570247

标签:numpngw,matplotlib,png动画
0
投稿

猜你喜欢

  • Python 字符串操作实现代码(截取/替换/查找/分割)

    2023-07-14 06:14:00
  • python动态监控日志内容的示例

    2022-08-26 18:05:44
  • python利用元类和描述器实现ORM模型的详细步骤

    2023-11-13 14:54:12
  • Python 转移文件至云对象存储的方法

    2022-02-01 04:28:19
  • Python自定义聚合函数merge与transform区别详解

    2022-09-14 11:45:30
  • ActionScript3.0是革命性的

    2008-05-01 12:36:00
  • Python箱型图绘制与特征值获取过程解析

    2023-09-20 06:22:37
  • python 请求服务器的实现代码(http请求和https请求)

    2023-07-10 08:23:58
  • python使用Thread的setDaemon启动后台线程教程

    2023-09-29 05:49:26
  • Python图像处理模块ndimage用法实例分析

    2023-09-08 16:52:26
  • archlinux 罗技K380 F1-F12 功能键锁定(实现方法)

    2023-11-27 22:42:48
  • Python爬取奶茶店数据分析哪家最好喝以及性价比

    2021-02-19 08:41:45
  • 在OneProxy的基础上实行MySQL读写分离与负载均衡

    2024-01-12 22:54:28
  • MYSQL自定义函数判断是否正整数的实例代码

    2024-01-19 19:14:20
  • MySQL教程:Group By用法

    2009-02-26 15:27:00
  • python中使用PIL制作并验证图片验证码

    2023-06-05 11:28:33
  • kill一条TCP连接实现方法详解

    2023-06-06 05:45:43
  • 详解Python各大聊天系统的屏蔽脏话功能原理

    2021-02-23 13:53:44
  • 基于Python OpenCV和 dlib实现眨眼检测

    2021-05-28 18:50:28
  • python实现知乎高颜值图片爬取

    2023-03-11 10:35:54
  • asp之家 网络编程 m.aspxhome.com