下拉選單

Python 的 Plotly 資料視覺化入門

Alex Scriven

Data Scientist

什麼是下拉選單?

 

  • 讓使用者從多個選項中選擇

  • 用來更新資料或版面元素

 

簡單下拉選單範例

Python 的 Plotly 資料視覺化入門

Plotly 的下拉選單

$$

fig = go.Figure()

for suburb in ['Ashfield', 'Lidcombe', 'Bondi Junction']: df = syd_houses[syd_houses.Suburb == suburb]
fig.add_trace( px.bar(df, x='Year', y='Median House Price').data[0])
  • 下拉選單透過顯示或隱藏特定 trace 運作
Python 的 Plotly 資料視覺化入門

隱藏 trace

$$

  • visible 參數決定 trace 是否可見(True)或隱藏(False
  • 使用 args 來更新不同 trace 的 visible 參數
args:[{'visible': [True, False, False]}]

 

資料與版面內部

Python 的 Plotly 資料視覺化入門

下拉選單物件

# Create the dropdown
dropdown_buttons = [
  {'label': 'Ashfield', 'method': 'update',

'args': [{'visible': [True, False, False]}, {'title': 'Ashfield'}]},
{'label': 'Lidcombe', 'method': 'update',
'args': [{'visible': [False, True, False]}, {'title': 'Lidcombe'}]},
{'label': 'Bondi Junction', 'method': 'update',
'args': [{'visible': [False, False, True]}, {'title': 'Bondi Junction'}]}
]
Python 的 Plotly 資料視覺化入門

加入下拉選單

$$

fig.update_layout({
  'updatemenus':[{
    'type': "dropdown",
    'x': 1.3,
    'y': 0.5,
    'showactive': True,
    'active': 0,
    'buttons': dropdown_buttons}]
})
fig.show()

$$

下拉選單房價圖

Python 的 Plotly 資料視覺化入門

一起來練習吧!

Python 的 Plotly 資料視覺化入門

Preparing Video For Download...