matplotlib の動的グラフのエクスポート方法を教えてください。
matplotlibのアニメーションをエクスポートするには、次の方法を使用します。
- funcAnimationメソッドでアニメーションを作成してから、saveメソッドを使ってアニメーション画像ファイルを保存します。例を次に示します。
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 创建图形和动画函数
fig, ax = plt.subplots()
def animate(i):
ax.cla() # 清除当前轴上的内容
ax.plot(x, y[i]) # 绘制新的帧
# 创建动画对象
ani = FuncAnimation(fig, animate, frames=len(y), interval=200)
# 保存为动态图像文件(例如gif)
ani.save('animation.gif', writer='pillow')
- matplotlib.animationモジュールのAnimationクラスとFFMpegWriterクラスを用いて、アニメーションのグラフを作成し保存します。例を以下に示します。
import matplotlib.pyplot as plt
from matplotlib.animation import Animation, FFMpegWriter
# 创建图形和动画函数
fig, ax = plt.subplots()
def animate(i):
ax.cla() # 清除当前轴上的内容
ax.plot(x, y[i]) # 绘制新的帧
# 创建动画对象
ani = Animation(fig, animate, frames=len(y), interval=200)
writer = FFMpegWriter(fps=10) # 创建写入器
# 保存为动态图像文件(例如mp4)
ani.save('animation.mp4', writer=writer)
これらの手法を使えば、matplotlibのアニメーションをGIF、mp4など、さまざまなフォーマットのファイルとして保存することができます。