はじめに
matplotlib FuncAnimationでスノーノイズのアニメーションを作成する。
コード
%matplotlib notebook | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.animation as animation | |
from IPython.display import HTML | |
fig, ax = plt.subplots() | |
plt.tick_params(labelbottom=False, labelleft=False) | |
plt.tick_params(color='white') | |
def update(num): | |
ax.cla() | |
snow_n = plt.imshow(data[:,:,num], cmap='binary', vmin=0, vmax=1) | |
data = np.random.randint(0,2,(240,320,10)) # 4:3 ブラウン管TV | |
ani = animation.FuncAnimation(fig, update, 10, interval=100) | |
HTML(ani.to_html5_video()) | |
#dpi=100 | |
#ani.save('snownoise.mp4', writer="ffmpeg",dpi=dpi) |
解説
モジュールのインポート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
図と軸と軸ラベルの設定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig, ax = plt.subplots()
plt.tick_params(labelbottom=False, labelleft=False)
plt.tick_params(color='white')
軸のラベルを非表示にし、軸の目盛を白くして見えないようにしている。
スノーノイズデータの生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = np.random.randint(0,2,(240,320,10)) # 4:3 ブラウン管TV
スノーノイズとは、ブラウン管TVでアナログテレビ放送を受信する際に生じるノイズのことである。
np.random.randint(0,2,(240,320,10)) で0または1のランダムなデータ群を生成する。データの構造は、(240,320)のデータが10個ある構造となっている。
データのサイズについては、ブラウン管TVのアスペクト比とおなじになるように4:3とした。
アニメーションの設定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update(num):
ax.cla()
snow_n = plt.imshow(data[:,:,num], cmap='binary', vmin=0, vmax=1)
アニメーションでは古い画像を消して新しい画像を表示するという動作を繰り返すので、ax.cla()で現在表示されている画像を消している。
plt.imshow(data[:,:,num], cmap=’binary’, vmin=0, vmax=1)で白黒のスノーノイズ画像をnumに従って、順次表示していく。つまり、表示されるdataは、data[:,:,0]、data[:,:,1]、data[:,:,2]という風になる。
アニメーションの表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ani = animation.FuncAnimation(fig, update, 10, interval=100)
HTML(ani.to_html5_video())
FuncAnimationでanimationの表示。updateを10step実行してアニメーションとする。intervalは100 ms なので1秒のアニメーションとなる。
HTML(ani.to_html5_video())でjupyter notebook上にアニメーションを表示できる。
アニメーションの保存
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#dpi=100
#ani.save('snownoise.mp4', writer="ffmpeg",dpi=dpi)
ani.save(‘snownoise.mp4’, writer=”ffmpeg”,dpi=dpi)でアニメーションを保存することができる。dpiを設定することで指定の解像度での保存が可能。
ffmpegのインストールが必要な場合は、
conda install -c anaconda ffmpeg
によりインストールする。(condaでモジュールの管理をしている場合)
コードをダウンロード(.pyファイル) コードをダウンロード(.ipynbファイル)参考

コメント