LoginSignup
20
19

More than 5 years have passed since last update.

ipython-notebookでアニメーション

Last updated at Posted at 2016-01-04

airtoxin/ipython-animated-array
https://gyazo.com/5e0f68746ce124a0891749903b6c8a3c

こんなやつです。Jupyterも可。

$ pip install ipython-animated-arrayでインストールできます。

AnimateArrayクラスのコンストラクタにnumpy.arrayの配列を渡しshow()すると配列の中のそれぞれのnumpy.arrayがアニメーションの1フレームとなり、いい感じにアニメーションしてくれます。
cmap引数は色付けで、ここにあるやつがだいたい使えます。matplotlibのバージョンの関係で最近追加されたものは対応していません…。
show()にreflesh引数で数字を渡すとアニメーションの更新間隔が変更できます。デフォルトは1000(ms)です。

本当はgithubの.ipynbプレビューでもアニメーションするようにしたかったのですがjsが動かないのでダメでした…。


from ipython_animated_array import AnimateArray
import numpy as np


initial = np.random.randint(2, size=(10,10))
AnimateArray([initial], cmap='winter').show()

def get_next(current):
    next_state = []
    for i in range(10):
        next_row = []
        for j in range(10):
            living = current[i-1][j-1] + current[i-1][j] + current[i-1][(j+1)%10] + \
                        current[i][j-1] + current[i][j] + current[i][(j+1)%10] + \
                        current[(i+1)%10][j-1] + current[(i+1)%10][j] + current[(i+1)%10][(j+1)%10]
            next_row.append(1 if living in (3,4) else 0)
        next_state.append(next_row)
    return next_state

states = [initial]
current = initial
for i in range(1000):
    current = get_next(current)
    states.append(np.array(current))

AnimateArray(states, cmap='winter').show(reflesh=400)

こんな感じのコードを実行するとライフゲームっぽいのもすぐに出せます。

https://gyazo.com/170088a47a42390bb62460d913d3d4b3

以上、正月の進捗でした。

20
19
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
20
19