Dropdowns

การแสดงผลข้อมูลด้วย Plotly ใน Python

Alex Scriven

Data Scientist

Dropdown คืออะไร?

 

  • ให้ผู้ใช้เลือกจากตัวเลือกที่กำหนด

  • อัปเดตข้อมูลหรือองค์ประกอบเลย์เอาต์

 

ตัวอย่าง dropdown อย่างง่าย

การแสดงผลข้อมูลด้วย Plotly ใน Python

Dropdown ใน 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 ทำงานโดยการแสดงและซ่อน trace ที่ระบุ
การแสดงผลข้อมูลด้วย Plotly ใน Python

การซ่อน trace

$$

  • อาร์กิวเมนต์ visible กำหนดว่า trace จะแสดง (True) หรือซ่อน (False)
  • ใช้ args เพื่ออัปเดตอาร์กิวเมนต์ visible ของ trace ต่างๆ
args:[{'visible': [True, False, False]}]

 

โครงสร้างข้อมูลภายใน

การแสดงผลข้อมูลด้วย Plotly ใน Python

ออบเจกต์ 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'}]}
]
การแสดงผลข้อมูลด้วย Plotly ใน Python

การเพิ่ม dropdown

$$

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

$$

Dropdown ราคาบ้าน

การแสดงผลข้อมูลด้วย Plotly ใน Python

มาฝึกกันเถอะ!

การแสดงผลข้อมูลด้วย Plotly ใน Python

Preparing Video For Download...