使用numpngw和matplotlib生成png動(dòng)畫的示例代碼
在matplotlib官網(wǎng)看到了第三方庫numpngw的簡(jiǎn)介,利用該庫作為插件可以輔助matplotlib生成png動(dòng)畫。
numpngw概述
numpngw庫可生成PNG靜態(tài)圖像和PNG動(dòng)畫。
- 通過write_png函數(shù)可以將 numpy保存為PNG 文件。
- 通過 write_apng 函數(shù)可以將數(shù)組序列保存為 PNG 動(dòng)畫(APNG)文件 。
- 通過AnimatedPNGWriter類可以將Matplotlib 保存為PNG動(dòng)畫文件。
numpngw庫的依賴包是numpy和setuptools。
使用numpngw和matplotlib生成png動(dòng)畫
numpngw+matplotlib實(shí)現(xiàn)png動(dòng)畫
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實(shí)現(xiàn)gif動(dòng)畫
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)鍵代碼解讀
# 導(dǎo)入AnimatedPNGWriter
from numpngw import AnimatedPNGWriter
# 初始化AnimatedPNGWriter
writer = AnimatedPNGWriter(fps=12)
# 將save函數(shù)中的writer參數(shù)設(shè)為AnimatedPNGWriter實(shí)例
animator.save("love.png", writer=writer)
通過對(duì)比可知,使用 numpngw+matplotlib生成png動(dòng)畫方式非常簡(jiǎn)單,只用初始化AnimatedPNGWriter,在save函數(shù)中指定writer即可。
到此這篇關(guān)于使用numpngw和matplotlib生成png動(dòng)畫的示例代碼的文章就介紹到這了,更多相關(guān)numpngw和matplotlib生成png動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 產(chǎn)生token及token驗(yàn)證的方法
今天小編就為大家分享一篇python 產(chǎn)生token及token驗(yàn)證的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python使用Matplotlib繪制甘特圖的實(shí)踐
甘特圖已經(jīng)發(fā)展成項(xiàng)目規(guī)劃和跟蹤的必備工具,本文主要介紹了Python使用Matplotlib繪制甘特圖的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Python 20行簡(jiǎn)單實(shí)現(xiàn)有道在線翻譯的詳解
這篇文章主要介紹了Python實(shí)現(xiàn)有道在線翻譯的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python3 selenium 實(shí)現(xiàn)QQ群接龍自動(dòng)化功能
這篇文章主要介紹了Python3 selenium 實(shí)現(xiàn)QQ群接龍自動(dòng)化功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

