서브플롯

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

Alex Scriven

Data Scientist

서브플롯이란?

 

  • 서브플롯: 그리드에 배치된 ‘미니 플롯’

$$

  • 동일 데이터의 다른 플롯 유형 또는 다른 하위집합 표시

 

서브플롯 개요

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

트레이스 복습

 

  • Plotly 그림은 traces 목록을 가집니다: 데이터와 유형

  • fig.data[0], fig.data[1]로 접근

    • .add_trace()로 서브플롯에 추가 가능

$$

px_fig = px.scatter(df...)
print(px_fig)
Figure({'data': [trace1], 'layout': {...}})
Python으로 배우는 Plotly 데이터 시각화 입문

1x2 서브플롯 생성

import plotly.express as px
from plotly.subplots import make_subplots

# Create a subplot grid
fig = make_subplots(rows=2, cols=1)

# Create plotly express figures hist = px.histogram(revenues, x='Revenue') box = px.box(revenues, x='Revenue')
# Extract traces and add to subplots fig.add_trace(hist.data[0], row=1, col=1) fig.add_trace(box.data[0], row=2, col=1) fig.show()

 

금융 서브플롯

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

서브플롯 사용자 지정

 

  • 전체 플롯 제목 없음
  • 서브플롯 제목 없음

$$

$$

$$

  • ✨ 발표용으로 다듬기

 

간단한 금융 서브플롯

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

서브플롯 제목

from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=1,
    subplot_titles=[
    '회사 매출의 히스토그램', 
    '회사 매출의 박스 플롯'])

## Add in traces (fig.add_trace())
fig.update_layout({'title': {'text': '회사 매출 플롯', 'x': 0.5, 'y': 0.9}}) fig.show()

서브플롯 제목 수정됨

자세한 옵션은 문서에서 확인하십시오

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

세로 스택 서브플롯

$$

fig = make_subplots(rows=3, cols=1, 
  subplot_titles=['Adelie Penguins'
  , 'Gentoo Penguins', 'Chinstrap Penguins'])

row_num = 1 for species in ['Adelie', 'Gentoo', 'Chinstrap']: # Filter data for this species df = penguins[penguins['Species'] == species]
scatter = px.scatter(df, x='Culmen Length (mm)' , y='Culmen Depth (mm)') # Add the trace to the subplot fig.add_trace(scatter.data[0] , row=row_num, col=1) row_num +=1

펭귄 서브플롯(세로 스택)

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

축 공유 서브플롯

 

  • x축 공유 설정:
fig = make_subplots(
    rows=3, cols=1
    , shared_xaxes=True)

x축 공유 펭귄 서브플롯

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

연습해 봅시다!

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

Preparing Video For Download...