読み込んだデータを時系列表示する際に,逐次(1秒ごと)に追加(更新)してプロットするサンプルプログラム
出力プロット例
コードブロック
add_plotting.py
import time
import random
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# ランダムな時系列データを生成
data = [random.randint(0, 10) for _ in range(10)]
# FigureとAxesを作成
fig, ax = plt.subplots()
line, = ax.plot([], [])
# Axesの設定
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Value")
# アニメーションの更新関数
def update(frame):
x = list(range(frame + 1))
y = data[:frame + 1]
line.set_data(x, y)
return line,
# アニメーションの生成と表示
ani = animation.FuncAnimation(fig, update, frames=10, interval=1000, blit=True)
plt.show()