X-Yプロットをアニメーション表示するサンプル
XY_plot_anim.py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# サンプル時系列加速度データを生成(ここに実際のデータを使用)
t = np.linspace(0, 10, 100)
acceleration = np.sin(2 * np.pi * t) # 加速度データの例
# フィギュアとプロットの作成
fig, ax = plt.subplots()
ax.set_xlim(0, 10)
ax.set_ylim(-1.5, 1.5)
line, = ax.plot([], [], lw=2)
# 初期化関数
def init():
line.set_data([], [])
return (line,)
# アニメーション関数
def animate(i):
x = t[:i]
y = acceleration[:i]
line.set_data(x, y)
return (line,)
# アニメーションの設定
ani = animation.FuncAnimation(fig, animate, init_func=init, frames=len(t), interval=100, blit=True)
# GIFとして保存
ani.save("animation.gif", writer='pillow', fps=10)
# アニメーションの表示
plt.show()