カスタムボタン

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

Alex Scriven

Data Scientist

カスタムボタンの機能

 

カスタムボタンでできること:

  • プロットのデータやレイアウト要素を更新

    • update_layout()の調整はボタンにまとめられる
  • アニメーションを補助(本コース範囲外)

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

Plotlyのカスタムボタン

updatemenus 引数で追加し、次を指定:

  • type: buttons または dropdown
    • ドロップダウンは後ほど解説
  • direction: ボタンの向き
    • 横並び(left)か縦並び(down
  • x/y: 位置を指定する小数
  • showactive: True/Falseactive(押下中のボタンのインデックス)を表示
    • アクティブボタンは現在選択中
  • buttons: button オブジェクトのリスト
Pythonで学ぶPlotlyによるデータ可視化入門

ボタン付きのプロット種類

$$

fig = px.bar(
      data_frame=revenues, 
    x='Industry', y='Revenue',
    color='Industry')
fig.show()

業種別の単純な棒グラフ

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

ボタンの設定

 

my_buttons = [

{'label': "Bar plot",
'method': "update",
'args': [{"type": "bar"}]},
{'label': "scatterplot",
'method': "update",
'args': [{"type": "scatter", 'mode': 'markers'}]}
]
Pythonで学ぶPlotlyによるデータ可視化入門

args 引数

Plotlyで最も混乱しやすい部分の1つ!

  • 構造は次のとおり:

[{dictionary to send to data}, {dictionary to send to layout}]

図の内部構造

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

argsでレイアウトを更新

dir(fig.layout)

Figure layout elements

  • 'args'で任意の引数を更新可能
Pythonで学ぶPlotlyによるデータ可視化入門

argsでデータを更新

dir(fig.data[0])

Inside data layout

  • 見慣れた項目もあり、後で役立つものもある
Pythonで学ぶPlotlyによるデータ可視化入門

ボタンのインタラクション

$$

fig.update_layout({
  'updatemenus': [{'type': "buttons",

'direction': 'down', 'x': 1.3, 'y': 0.5,
'showactive': True, 'active': 0,
'buttons': my_buttons}] }) fig.show()

$$

ボタン

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

練習してみましょう!

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

Preparing Video For Download...