2021-02-06 Pythonは、matplotlibパッケージとgifパッケージを介してgifアニメーションを生成します

Pythonはmatplotlibパッケージとgifパッケージを介してgifアニメーションを生成します

使用matplotlibアニメーションGIFを生成するには多くの方法があり、通常matplotlibanimationモジュールFuncAnimation関数の実装で使用さますmatplotlib私が見たgifの導入、サードパーティ製のアニメーションパッケージ公式サイトを

gifパッケージの概要

gifパッケージには、アニメーションの拡張サポート Altair、 matplotlibおよびPlotly
gif依存性PIL、すなわちpillow、要件Pillow>=7.1.2
インストールgifパッケージ、pip install gif

アニメーションの原理

すべて动画が構成帧(frame)され、フレームは静止画であり、連続するフレームはアニメーションを形成します。通常、フレーム数と言いますが、簡単に言えば、1秒間に送信される画像のフレーム数です。グラフィックプロセッサは1秒間に数回リフレッシュできることも理解できます。これは通常fps(Frames Per Second)表現されます。アニメーションを作成するための鍵:フレームの生成方法と1秒あたりのフレーム数。

gifパッケージの解釈

gifパッケージは非常に簡潔で、ファイルは1つだけgif.pyで、ファイルには主にoptionsクラスframessave2つの関数が含まれています

optionsクラス

要約版を提供しAltair、 matplotlibそしてPlotly設定を保存またはエクスポート。matplotlibとして、次の設定を提供します。

  • dpi(int):1インチあたりのドット数で表した解像度

  • facecolor(colorspec):フィギュアのフェイスカラー

  • edgecolor(colorspec):フィギュアのエッジカラー

  • 透明(ブール):Trueの場合、軸パッチはすべて透明になります

設定方法: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 animation

framesつまり、@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類似しており、より低レベルであるため、より複雑な図面に遭遇した場合により柔軟であることがわかります。

おすすめ

転載: blog.csdn.net/qingfengxd1/article/details/113725721