JupyterNotebookで、アニメーションするグラフを簡単に作る(plotly)
目的
- JupyterNotebook上で、グラフをアニメーションさせたい。
- 面倒くさいインターフェースはいやだ。簡単に試したい。
想定されるシチュエーション
- 損失関数の推移
- 最適化の経緯
- 発表とか勉強会用に、理解しやすいように使うのが主かもしれません。
コード全量
GitHub
https://github.com/tatsuya-takahashi/python-library-exercises/blob/master/plotly/plotly.ipynb
Jupyter nbviwer(GitHubだと、plotlyが表示されない)
http://nbviewer.jupyter.org/github/tatsuya-takahashi/python-library-exercises/blob/master/plotly/plotly.ipynb
結論
# import plotly as offline mode
import plotly.offline as offline
import plotly.graph_objs as go
offline.init_notebook_mode()
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
frames = []
xaxis = []
# framesに突っ込んだデータの通りに、勝手にアニメーションしてくれる。
# 今回は y = x **2のグラフを想定している。
for x in np.arange(-10,11,1):
xaxis.append(x)
data = [
go.Scatter(
x = xaxis,
y = np.array(xaxis) ** 2
)
]
frames.append({"data": data})
layout = go.Layout(
title="アニメーション サンプル",
xaxis={"title":"x", "range": [-10, 10]},
yaxis={"title":"y(x ** 2)", "range": [-100, 100]},
showlegend=True,
height=600,
updatemenus=[{'type': 'buttons',
'buttons': [{'label': '再生',
'method': 'animate',
'args': [None]}]}]
)
fig = go.Figure(
data=data,
layout=layout,
frames=frames
)
offline.iplot(fig)
このように表示されます
- 損失関数の推移
- 最適化の経緯
- 発表とか勉強会用に、理解しやすいように使うのが主かもしれません。
コード全量
GitHub
https://github.com/tatsuya-takahashi/python-library-exercises/blob/master/plotly/plotly.ipynb
Jupyter nbviwer(GitHubだと、plotlyが表示されない)
http://nbviewer.jupyter.org/github/tatsuya-takahashi/python-library-exercises/blob/master/plotly/plotly.ipynb
結論
# import plotly as offline mode
import plotly.offline as offline
import plotly.graph_objs as go
offline.init_notebook_mode()
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
frames = []
xaxis = []
# framesに突っ込んだデータの通りに、勝手にアニメーションしてくれる。
# 今回は y = x **2のグラフを想定している。
for x in np.arange(-10,11,1):
xaxis.append(x)
data = [
go.Scatter(
x = xaxis,
y = np.array(xaxis) ** 2
)
]
frames.append({"data": data})
layout = go.Layout(
title="アニメーション サンプル",
xaxis={"title":"x", "range": [-10, 10]},
yaxis={"title":"y(x ** 2)", "range": [-100, 100]},
showlegend=True,
height=600,
updatemenus=[{'type': 'buttons',
'buttons': [{'label': '再生',
'method': 'animate',
'args': [None]}]}]
)
fig = go.Figure(
data=data,
layout=layout,
frames=frames
)
offline.iplot(fig)
このように表示されます
GitHub
https://github.com/tatsuya-takahashi/python-library-exercises/blob/master/plotly/plotly.ipynb
Jupyter nbviwer(GitHubだと、plotlyが表示されない)
http://nbviewer.jupyter.org/github/tatsuya-takahashi/python-library-exercises/blob/master/plotly/plotly.ipynb
# import plotly as offline mode
import plotly.offline as offline
import plotly.graph_objs as go
offline.init_notebook_mode()
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
frames = []
xaxis = []
# framesに突っ込んだデータの通りに、勝手にアニメーションしてくれる。
# 今回は y = x **2のグラフを想定している。
for x in np.arange(-10,11,1):
xaxis.append(x)
data = [
go.Scatter(
x = xaxis,
y = np.array(xaxis) ** 2
)
]
frames.append({"data": data})
layout = go.Layout(
title="アニメーション サンプル",
xaxis={"title":"x", "range": [-10, 10]},
yaxis={"title":"y(x ** 2)", "range": [-100, 100]},
showlegend=True,
height=600,
updatemenus=[{'type': 'buttons',
'buttons': [{'label': '再生',
'method': 'animate',
'args': [None]}]}]
)
fig = go.Figure(
data=data,
layout=layout,
frames=frames
)
offline.iplot(fig)
このように表示されます
Author And Source
この問題について(JupyterNotebookで、アニメーションするグラフを簡単に作る(plotly)), 我々は、より多くの情報をここで見つけました https://qiita.com/iwashi-kun/items/4fbe4a60e566e0743e3e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .