슬라이더

Python으로 배우는 Plotly 데이터 시각화 입문

Alex Scriven

Data Scientist

슬라이더란?

 

  • 값을 전환하며 플롯을 갱신하는 인터랙티브 요소

  • 시간 경과 데이터 확인에 활용

  • 임의의 그룹에도 사용 가능

$$

$$

$$

  • 💡 플롯에 어울리는지 확인

 

연도 슬라이더:

연도 슬라이더

펭귄 섬 슬라이더:

펭귄 섬 슬라이더

Python으로 배우는 Plotly 데이터 시각화 입문

plotly.express의 슬라이더

 

  • animation_frame: 슬라이더에 표시할 값(이전 슬라이드의 Year 또는 Island)

$$

  • animation_group: 프레임 간 동일한 샘플을 식별
Python으로 배우는 Plotly 데이터 시각화 입문

슬라이더로 본 매출 vs. 직원 수

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

 

연도별 매출

Python으로 배우는 Plotly 데이터 시각화 입문

제한: animate 메서드

plotly.expressanimate 메서드로 슬라이더를 구현합니다

$$

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

$$

  • 동일한 데이터 포인트의 시간 변화만 애니메이션
  • 슬라이더를 트레이스로 직접 구성
Python으로 배우는 Plotly 데이터 시각화 입문

계획

 

  1. 필요한 트레이스로 figure 객체 생성
  2. 트레이스 표시/숨김을 위한 sliders 객체 정의
  3. 레이아웃을 업데이트해 figure에 슬라이더 추가
Python으로 배우는 Plotly 데이터 시각화 입문

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])
Python으로 배우는 Plotly 데이터 시각화 입문

슬라이더 만들기

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

$$

자세한 서식 옵션은 문서에서 확인하세요

Python으로 배우는 Plotly 데이터 시각화 입문

슬라이더 추가하기

 

$$

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

펭귄 섬 슬라이더 최종

Python으로 배우는 Plotly 데이터 시각화 입문

연습해 봅시다!

Python으로 배우는 Plotly 데이터 시각화 입문

Preparing Video For Download...