Dropdown

Pengantar Visualisasi Data dengan Plotly di Python

Alex Scriven

Data Scientist

Apa itu dropdown?

 

  • Memungkinkan pengguna memilih dari beberapa opsi

  • Memperbarui data atau elemen layout

 

Contoh dropdown sederhana

Pengantar Visualisasi Data dengan Plotly di Python

Dropdown di 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])
  • Dropdown bekerja dengan menampilkan dan menyembunyikan trace tertentu
Pengantar Visualisasi Data dengan Plotly di Python

Menyembunyikan trace

$$

  • Argumen visible menentukan apakah trace terlihat (True) atau tidak (False)
  • Gunakan args untuk memperbarui visible pada berbagai trace
args:[{'visible': [True, False, False]}]

 

Di dalam data layout

Pengantar Visualisasi Data dengan Plotly di Python

Objek dropdown

# 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'}]}
]
Pengantar Visualisasi Data dengan Plotly di Python

Menambahkan dropdown

$$

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

$$

Dropdown harga rumah

Pengantar Visualisasi Data dengan Plotly di Python

Ayo berlatih!

Pengantar Visualisasi Data dengan Plotly di Python

Preparing Video For Download...