LoginSignup
15
16

More than 5 years have passed since last update.

matplotlibで複数のグラフをアニメーションさせる

Posted at

matplotlibで複数の図形をアニメーションさせるサンプルを書きました。
アニメーション前に動かしたい図形を描画しておかないとエラーが出ます。

サンプルコード

#!/usr/local/bin/python3.5
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure(1)
ax = fig.add_subplot(111)

#アニメーションさせる前に初期状態を描いておく
x = np.arange(0, 10, 0.1)
y = np.sin(x)
sin01, = ax.plot(y, 'b')
sin02, = ax.plot(y, 'g')

#フレーム更新
def update(i):
    sin01.set_ydata(np.sin(x-i))
    sin02.set_ydata(np.sin(x+i))

ani = animation.FuncAnimation(fig, update, 1000, blit=False, interval=100, repeat=False)
#ani.save('./animation.mp4')

plt.show()

結果

animation.gif

15
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
16