2021-02-06 Python通过matplotlib包和gif包生成gif动画
Python通過(guò)matplotlib包和gif包生成gif動(dòng)畫(huà)
使用matplotlib生成gif動(dòng)畫(huà)的方法有很多,一般常規(guī)使用matplotlib的animation模塊的FuncAnimation函數(shù)實(shí)現(xiàn)。在matplotlib官網(wǎng)看到了第三方動(dòng)畫(huà)包gif的介紹。
gif包概述
gif包是支持?Altair,?matplotlib和Plotly的動(dòng)畫(huà)擴(kuò)展。
gif依賴PIL,即pillow,要求Pillow>=7.1.2。
安裝gif包,pip install gif
動(dòng)畫(huà)原理
所有動(dòng)畫(huà)都是由幀(frame)構(gòu)成的,一幀就是一幅靜止的畫(huà)面,連續(xù)的幀就形成動(dòng)畫(huà)。我們通常說(shuō)幀數(shù),簡(jiǎn)單地說(shuō),就是在1秒鐘時(shí)間里傳輸?shù)膱D片的幀數(shù),也可以理解為圖形處理器每秒鐘能夠刷新幾次,通常用fps(Frames Per Second)表示。制作動(dòng)畫(huà)的關(guān)鍵:如何生成幀,每秒多少幀。
gif包解讀
gif包非常簡(jiǎn)潔,只有一個(gè)單獨(dú)的文件gif.py,文件主要包含options類、frames和save兩個(gè)函數(shù)。
options類
提供精簡(jiǎn)版 的Altair,?matplotlib和Plotly的保存或輸出設(shè)置。以matplotlib為例,提供以下設(shè)置。
-
dpi (int): The resolution in dots per inch
-
facecolor (colorspec): The facecolor of the figure
-
edgecolor (colorspec): The edgecolor of the figure
-
transparent (bool): If True, the axes patches will all be transparent
設(shè)置方法:gif.options.matplotlib["dpi"] = 300
原理:options在構(gòu)造函數(shù)中創(chuàng)建matplotlib字典保存配置,隨后傳遞給底層的matplotlib包。
frames函數(shù)
裝飾器函數(shù),通過(guò)對(duì)應(yīng)包編寫(xiě)自定義繪圖函數(shù)生成單幀圖像。
save函數(shù)
根據(jù)幀序列生成動(dòng)畫(huà)。
def save(frames, path, duration=100, unit="milliseconds", between="frames", loop=True): """Save decorated frames to an animated gif. - frames (list): collection of frames built with the gif.frame decorator - path (str): filename with relative/absolute path - duration (int/float): time (with reference to unit and between) - unit {"ms" or "milliseconds", "s" or "seconds"}: time unit value - between {"frames", "startend"}: duration between "frames" or the entire gif ("startend") - loop (bool): infinitely loop the animationframes即根據(jù)@gif.frame裝飾的繪圖函數(shù)生成的幀的序列,此處根據(jù)需要自定義。
duration即持續(xù)時(shí)間,由單位unit和模式between決定,默認(rèn)為frames為幀間的時(shí)間間隔。
unit即持續(xù)時(shí)間單位,支持毫秒和秒,默認(rèn)為毫秒。
between即持續(xù)時(shí)間計(jì)算模式,默認(rèn)frames即duration為幀之間的時(shí)間間隔,startend模式時(shí)duration=duration /len(frames),即duration為所有幀—整個(gè)動(dòng)畫(huà)的持續(xù)時(shí)間。
gif包生成gif動(dòng)畫(huà)實(shí)踐
?
import randomfrom matplotlib import pyplot as pltimport gif # 構(gòu)造數(shù)據(jù)x = [random.randint(0, 100) for _ in range(100)]y = [random.randint(0, 100) for _ in range(100)]# 設(shè)置選項(xiàng)gif.options.matplotlib["dpi"] = 300# 使用gif.frame裝飾器構(gòu)造繪圖函數(shù),即如何生成靜態(tài)的幀@gif.framedef plot(i): xi = x[i * 10:(i + 1) * 10] yi = y[i * 10:(i + 1) * 10] plt.scatter(xi, yi) plt.xlim((0, 100)) plt.ylim((0, 100))# 構(gòu)造幀序列frames,即把生成動(dòng)畫(huà)的所有幀按順序放在列表中frames = []for i in range(10): frame = plot(i) frames.append(frame)# 根據(jù)幀序列frames,動(dòng)畫(huà)持續(xù)時(shí)間duration,生成gif動(dòng)畫(huà)gif.save(frames, 'example.gif', duration=3.5, unit="s", between="startend")以心形曲線為例比較gif包和animation模塊實(shí)現(xiàn)動(dòng)畫(huà)的差異
心形曲線繪制
from matplotlib import pyplot as pltimport numpy as np t = np.linspace(0, 6, 100)x = 16 * np.sin(t) ** 3y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)fig = plt.figure(figsize=(5, 3), dpi=100)plt.scatter(x, y)plt.show()gif包的實(shí)現(xiàn)方式
import numpy as npimport giffrom matplotlib import pyplot as plt t = np.linspace(0, 6, 100)x = 16 * np.sin(t) ** 3y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t) @gif.framedef plot_love(x, y): plt.figure(figsize=(5, 3), dpi=100) plt.scatter(x, y, 60, c="r", alpha=0.7, marker=r"$\heartsuit$") plt.axis("off") frames = []for i in range(1, len(x)): of = plot_love(x[:i], y[:i]) frames.append(of) gif.save(frames, "love.gif", duration=80)matplotlib?常規(guī)FuncAnimation函數(shù)實(shí)現(xiàn)方式
???????
from matplotlib import pyplot as pltimport matplotlib.animation as animationimport numpy as np t = np.linspace(0, 6, 100)x = 16 * np.sin(t) ** 3y = 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')matplotlib底層PillowWriter類實(shí)現(xiàn)方式
from matplotlib import pyplot as pltimport matplotlib.animation as animationimport numpy as np t = np.linspace(0, 6, 100)x = 16 * np.sin(t) ** 3y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)def plot_love(x, y): 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 = animation.PillowWriter(fps=15)with writer.saving(fig, "love21.gif", dpi=100): for i in range(1, len(x)): plot_love(x[i], y[i]) writer.grab_frame()通過(guò)比較可知gif包的實(shí)現(xiàn)方式和matplotlib中利用PillowWriter實(shí)現(xiàn)方式類似,更偏底層一些,這樣遇到比較復(fù)雜的繪圖時(shí)更靈活。
總結(jié)
以上是生活随笔為你收集整理的2021-02-06 Python通过matplotlib包和gif包生成gif动画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2021-01-28 粒子群优化算法-P
- 下一篇: 2021-02-07 接口测试工具-Po