2022年6月5日日曜日

VS Code上のjupyter notebookでmatplotlibのアニメーションを再生する方法(How to play matplotlib's animation with jupyter notebook on VS Code)

こんにちは、WhiteTiger-21です。

タイトル通りの答えは(The answer of title is below)

from IPython.display import HTML
from matplotlib import animation, rc
from matplotlib import pyplot as plt

"""
animへアニメーション処理を行う
"""

HTML(anim.to_jshtml())

です。

細かい話を順に追って

matplotlibでアニメーションを表示する際に、普通は


from matplotlib import animation, rc
from matplotlib import pyplot as plt

def init():
	"""
    アニメーションの処理
    """

def animate(i):
	"""
    アニメーションの処理
    """

"""
ゴニョゴニョしたなにか
"""

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=Nt, interval=1000*(t[2]-t[1])*0.8, blit=True)

plt.show()


という感じで書きますよね。

ただ、jupyter notebookでは

 

from IPython.display import HTML
from matplotlib import animation, rc
from matplotlib import pyplot as plt

def init():
	"""
    アニメーションの処理
    """

def animate(i):
	"""
    アニメーションの処理
    """

"""
ゴニョゴニョしたなにか
"""

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=Nt, interval=1000*(t[2]-t[1])*0.8, blit=True)
HTML(anim.to_html5_video())

という感じで書きますよね。

 これでは画面に真っ黒なプレイヤーの画像が出るだけで再生されません。

これの原因は、VS Codeではビデオコーデックが入っていないからだそうです。

Cannot playback video clip in Jupyter within VSCode #7753 

その参照で、別のシステム(plotly)を使うことをおすすめされていますが、matplotlibで構築したシステムならば、こちらを使うほうが楽ですよね。

 

というわけで、

HTML(anim.to_jshtml())

を使うことがVS Code上でjupyter notebookを動かし、アニメーションを再生する方法として楽なものだと思います。

0 件のコメント:

コメントを投稿