スライダー

Pythonで学ぶPlotlyによるデータ可視化入門

Alex Scriven

Data Scientist

スライダーとは

 

  • 値を切替えてプロットを更新するインタラクティブ要素

  • 時系列の閲覧に便利

  • 任意のグループに使用可能

$$

$$

$$

  • 💡 プロットに適しているか確認

 

年スライダー:

年スライダー

ペンギンの島スライダー:

ペンギンの島スライダー

Pythonで学ぶPlotlyによるデータ可視化入門

plotly.express のスライダー

 

  • animation_frame スライダーに載せる項目(前スライドのYearIsland

$$

  • animation_group: フレーム間で同一のサンプルを識別
Pythonで学ぶPlotlyによるデータ可視化入門

収益 vs. 従業員数(スライダー付き)

fig = px.scatter(
  data_frame=revenues, 
  y='Revenue',
  x='Employees',
  color='Industry',

animation_frame='Year', animation_group='Company')
fig.update_layout({ 'yaxis': {'range': [0, 500000]}, 'xaxis': {'range': [-100000, 2500000]} })
fig['layout'].pop('updatemenus') fig.show()

 

年別の収益

Pythonで学ぶPlotlyによるデータ可視化入門

制約: animate メソッド

plotly.expressanimate メソッドでスライダーを実装します

$$

fig['layout']['sliders'][0].steps[0]['method']
animate

$$

  • 同一のデータ点のみを時系列でアニメーション
  • トレースでスライダーを構築
Pythonで学ぶPlotlyによるデータ可視化入門

計画

 

  1. 必要なトレースで図オブジェクトを作成
  2. トレースの表示/非表示を切替えるスライダーを定義
  3. レイアウトを更新して図にスライダーを追加
Pythonで学ぶPlotlyによるデータ可視化入門

図の作成

 

fig = go.Figure()

for island in ['Torgersen', 'Biscoe', 'Dream']: df = penguins[penguins.Island == island]
temp_trace = px.scatter(df, x="Culmen Length (mm)", y="Culmen Depth (mm)") fig.add_trace(temp_trace.data[0])
Pythonで学ぶPlotlyによるデータ可視化入門

スライダーの作成

sliders = [
  {'steps':[

{'method': 'update', 'label': 'Torgersen',
'args': [{'visible': [True, False, False]}]},
{'method': 'update', 'label': 'Bisco',
'args': [{'visible': [False, True, False]}]},
{'method': 'update', 'label': 'Dream',
'args': [{'visible': [False, False, True]}]}
]} ]

$$

詳細な書式設定はドキュメントを参照

Pythonで学ぶPlotlyによるデータ可視化入門

スライダーの追加

 

$$

fig.update_layout({'sliders': sliders})
fig.show()

ペンギンの島スライダー(最終)

Pythonで学ぶPlotlyによるデータ可視化入門

練習しましょう!

Pythonで学ぶPlotlyによるデータ可視化入門

Preparing Video For Download...