溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用numpngw與matplotlib怎么生成一個png動畫

發(fā)布時間:2021-01-25 15:58:01 來源:億速云 閱讀:178 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)使用numpngw與matplotlib怎么生成一個png動畫,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

numpngw概述

numpngw庫可生成PNG靜態(tài)圖像和PNG動畫。

  • 通過write_png函數(shù)可以將 numpy保存為PNG 文件。

  • 通過 write_apng 函數(shù)可以將數(shù)組序列保存為 PNG 動畫(APNG)文件 。

  • 通過AnimatedPNGWriter類可以將Matplotlib 保存為PNG動畫文件。

numpngw庫的依賴包是numpy和setuptools。

使用numpngw和matplotlib生成png動畫

numpngw+matplotlib實現(xiàn)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實現(xiàn)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')

關(guān)鍵代碼解讀

# 導入AnimatedPNGWriter
from numpngw import AnimatedPNGWriter

# 初始化AnimatedPNGWriter
writer = AnimatedPNGWriter(fps=12)
# 將save函數(shù)中的writer參數(shù)設(shè)為AnimatedPNGWriter實例
animator.save("love.png", writer=writer)

看完上述內(nèi)容,你們對使用numpngw與matplotlib怎么生成一個png動畫有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI