2021-02-06 Python通过matplotlib包和gif包生成gif动画
Python通過matplotlib包和gif包生成gif動畫
使用matplotlib生成gif動畫的方法有很多,一般常規使用matplotlib的animation模塊的FuncAnimation函數實現。在matplotlib官網看到了第三方動畫包gif的介紹。
gif包概述
gif包是支持?Altair,?matplotlib和Plotly的動畫擴展。
gif依賴PIL,即pillow,要求Pillow>=7.1.2。
安裝gif包,pip install gif
動畫原理
所有動畫都是由幀(frame)構成的,一幀就是一幅靜止的畫面,連續的幀就形成動畫。我們通常說幀數,簡單地說,就是在1秒鐘時間里傳輸的圖片的幀數,也可以理解為圖形處理器每秒鐘能夠刷新幾次,通常用fps(Frames Per Second)表示。制作動畫的關鍵:如何生成幀,每秒多少幀。
gif包解讀
gif包非常簡潔,只有一個單獨的文件gif.py,文件主要包含options類、frames和save兩個函數。
options類
提供精簡版 的Altair,?matplotlib和Plotly的保存或輸出設置。以matplotlib為例,提供以下設置。
-
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
設置方法:gif.options.matplotlib["dpi"] = 300
原理:options在構造函數中創建matplotlib字典保存配置,隨后傳遞給底層的matplotlib包。
frames函數
裝飾器函數,通過對應包編寫自定義繪圖函數生成單幀圖像。
save函數
根據幀序列生成動畫。
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即根據@gif.frame裝飾的繪圖函數生成的幀的序列,此處根據需要自定義。
duration即持續時間,由單位unit和模式between決定,默認為frames為幀間的時間間隔。
unit即持續時間單位,支持毫秒和秒,默認為毫秒。
between即持續時間計算模式,默認frames即duration為幀之間的時間間隔,startend模式時duration=duration /len(frames),即duration為所有幀—整個動畫的持續時間。
gif包生成gif動畫實踐
?
import randomfrom matplotlib import pyplot as pltimport gif # 構造數據x = [random.randint(0, 100) for _ in range(100)]y = [random.randint(0, 100) for _ in range(100)]# 設置選項gif.options.matplotlib["dpi"] = 300# 使用gif.frame裝飾器構造繪圖函數,即如何生成靜態的幀@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))# 構造幀序列frames,即把生成動畫的所有幀按順序放在列表中frames = []for i in range(10): frame = plot(i) frames.append(frame)# 根據幀序列frames,動畫持續時間duration,生成gif動畫gif.save(frames, 'example.gif', duration=3.5, unit="s", between="startend")以心形曲線為例比較gif包和animation模塊實現動畫的差異
心形曲線繪制
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包的實現方式
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?常規FuncAnimation函數實現方式
???????
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類實現方式
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()通過比較可知gif包的實現方式和matplotlib中利用PillowWriter實現方式類似,更偏底層一些,這樣遇到比較復雜的繪圖時更靈活。
總結
以上是生活随笔為你收集整理的2021-02-06 Python通过matplotlib包和gif包生成gif动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021-01-28 粒子群优化算法-P
- 下一篇: 2021-02-21 Python Ea