Sliders

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

Alex Scriven

Data Scientist

Slider คืออะไร?

 

  • องค์ประกอบเชิงโต้ตอบสำหรับสลับค่าและอัปเดตกราฟ

  • ใช้สำหรับดูข้อมูลตามเวลา

  • ใช้กับกลุ่มใดก็ได้

$$

$$

$$

  • 💡 ตรวจสอบให้แน่ใจว่าเหมาะสมกับกราฟของคุณ

 

ตัวอย่าง slider แบบปี:

สไลเดอร์แบบปี

ตัวอย่าง slider แบบเกาะเพนกวิน:

สไลเดอร์แบบเกาะเพนกวิน

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

Slider ใน plotly.express

 

  • animation_frame กำหนดสิ่งที่จะแสดงบน slider (เช่น Year หรือ Island ในสไลด์ก่อนหน้า)

$$

  • animation_group: ระบุตัวอย่างที่คงที่ข้ามเฟรม
การแสดงผลข้อมูลด้วย Plotly ใน Python

รายได้เทียบกับพนักงานพร้อม slider

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()

 

รายได้แยกตามปี

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

ข้อจำกัด: เมธอด animate

plotly.express สร้าง slider โดยใช้เมธอด animate

$$

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

$$

  • แอนิเมตเฉพาะ จุดข้อมูลเดิม ตามเวลา
  • สร้าง slider โดยใช้ trace
การแสดงผลข้อมูลด้วย Plotly ใน Python

แผนการดำเนินงาน

 

  1. สร้าง figure object พร้อม trace ที่จำเป็น
  2. กำหนด sliders object เพื่อแสดง/ซ่อน trace
  3. อัปเดต layout เพื่อเพิ่ม slider ลงใน figure
การแสดงผลข้อมูลด้วย Plotly ใน Python

สร้าง figure

 

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])
การแสดงผลข้อมูลด้วย Plotly ใน Python

สร้าง slider

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]}]}
]} ]

$$

ดูตัวเลือกการจัดรูปแบบเพิ่มเติมได้ในเอกสารประกอบ

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

เพิ่ม slider

 

$$

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

slider สุดท้ายของเกาะเพนกวิน

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

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

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

Preparing Video For Download...