使用numpngw与matplotlib怎么生成一个png动画-创新互联

今天就跟大家聊聊有关使用numpngw与matplotlib怎么生成一个png动画,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联专注于企业全网整合营销推广、网站重做改版、涟源网站定制设计、自适应品牌网站建设、HTML5购物商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为涟源等各大城市提供网站开发制作服务。

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动画有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


标题名称:使用numpngw与matplotlib怎么生成一个png动画-创新互联
链接地址:http://lszwz.com/article/cccddp.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款