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秒間に送信される画像のフレーム数です。グラフィックプロセッサは1秒間に数回リフレッシュできることも理解できます。これは通常fps(Frames Per Second)
表現されます。アニメーションを作成するための鍵:フレームの生成方法と1秒あたりのフレーム数。
gif
パッケージの解釈
gif
パッケージは非常に簡潔で、ファイルは1つだけgif.py
で、ファイルには主にoptions
クラスframes
とsave
2つの関数が含まれています。
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 random
from matplotlib import pyplot as plt
import 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.frame
def 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 plt
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)
fig = plt.figure(figsize=(5, 3), dpi=100)
plt.scatter(x, y)
plt.show()
gif
パッケージの実装方法
import numpy as np
import gif
from matplotlib import pyplot as plt
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)
@gif.frame
def 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 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')
matplotlib
低レベルのPillowWriter
クラスの実装
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)
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()