金狮镖局 Design By www.egabc.com
在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即可。
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无使用numpngw和matplotlib生成png动画的示例代码的评论...
更新日志
2024年09月23日
2024年09月23日
- 翁立友.2008-恨孤单【大旗】【WAV+CUE】
- 群星.2009-Magic.Cover日粤精华3CD【环球】【WAV+CUE】
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]