Plotly Subpolt 만들기

Plotly Subpolt 만들기

Data Visualization
Plotly Subplt 만들기
Author

양성모

Published

September 13, 2023

Plotly Subplot

Python으로 여러개의 그래프를 비교하기 좋은 방법은 subplot을 이용한 방법인 것 같습니다. subplot을 만드는 방법과 subplot의 title작성하는 방법 그리고 subplot들에 대한 전체 title을 넣는 방법도 함께 정리합니다.

Plotly의 make_subplots함수로 subplot을 구성하고 ploty 그래프를 생성했습니다. add_trace()함수로 생성한 subplot에 그래프를 추가합니다.

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols = 2)

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="subplot 1"),
              row=1, col=1)
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[100, 200, 300], name="subplot 2"),
              row=1, col=2)

fig.show()

Subplot 세부설정

make_subplot의 세부 설정을 알아보고 어떻게 변경할 수 있는지 알아봅니다. 하나의 Y축을 2개의 subplot이 공유하도록 shared_yaxes를 사용합니다. 각각의 subplot의 크기는 column_widths로 변경할 수 있습니다. 2개의 subplot사이의 거리는 horizontal_spacing으로 정합니다.

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(
    rows=1, cols=2,
    shared_yaxes=True,
    horizontal_spacing=0.05,
    column_widths=[0.3, 0.7]
)


fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="subplot 1"),
              row=1, col=1)
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[10, 20, 30], name="subplot 2"),
              row=1, col=2)

fig.show()

참고

  1. https://chancoding.tistory.com/229