使用numpngw和matplotlib生成png动画的示例代码
作者:mighty13 发布时间:2023-06-15 13:38:21
标签:numpngw,matplotlib,png动画
在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
0
投稿
猜你喜欢
- 前言mysql默认安装的时候是有一个root用户,这个用户有最高权限,如果要开放数据库里面的数据给别人使用的话,一般是不会开放root用户给
- 如下所示:a = [0,1,2,3,4,5,6,7,8,9]b = a[i:j] 表示复制a[i]到a[j-1],以生成新的list对象b
- 本文实例讲述了Python面向对象程序设计之私有属性及私有方法。分享给大家供大家参考,具体如下:如果有一个对象,当需要对其进行修改属性时,有
- 前言Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypt
- 1,查看py文件内的所有成员及快速定位到代码块如果你的py文件代码超过200行,一定要采用这个方法,能大大提高你的代码查找效率。方法1:(1
- 定义和用法fopen() 函数打开文件或者 URL。如果打开失败,本函数返回 FALSE。语法fopen(filename,mode,inc
- 本文实例为大家分享了PHP实现统计代码行数小工具,供大家参考,具体内容如下为了方面统计编程代码行数,做了一个小工具。自动统计指定目录以及目录
- 由于最近测试需要录制系统界面的操作过程,因为都是全屏的操作,所以用python做一个简单的录屏小工具。实现过程也是比较简单,就是通过对屏幕操
- 可以,具体说明和代码见下: <%@ Language=VBScript %><%Option
- 升级pippip版本必须升级到20.0.4版本才能应用;方法一、在pycharm中对pip进行升级;方法二、通过命令进行升级python3.
- Python列表具有内置的 list.sort()方法,可以在原地修改列表。 还有一个 sorted()内置的函数从迭代构建一个新的排序列表
- Js代码:/*** 验证码*/function yzm(){var codeChars = new Array(0, 1, 2, 3, 4,
- 本文实例讲述了Python统计分析模块statistics用法。分享给大家供大家参考,具体如下:一 计算平均数函数mean()>>
- 这是一篇关于怎样精简代码和Asp 特征以获得最快执行速度的详细文章。对于一个急燥的用户来说,任何在按下用户按钮到结果出现在它们的屏幕之间的延
- 怎样才能将在表A取得的数据插入另一个表B中?(1)对于表A和表B两个表结构完全相同的话〔字段个数,相应字段的类型等等〕,可以使用 inser
- 像微博一类的平台上传图片时,平台都会添加一个水印,宣誓着对图片的所有权,我们自己的博客平台也可以给自己的图片添加上水印。还是用 Pillow
- mysql取json字符串字段下的某个键的值要求:mysql版本5.7及以上SELECT JSON_EXTRACT('{"
- 我就废话不多说了,大家还是直接看代码吧~type Animal struct { ID int64 Name string `gorm:&q
- 模板继承是ThinkPHP3.1.2版本添加的一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层。模板继承其
- 本文内容由经典论坛星级会员 lipengadmin 收集整理绝大部分属于转载,有很多还是出自咱们蓝色的经典论坛.*****[第1页目录]**