1. graph_objects(go) 사용

1-1. go.Figure에 그래프적 요소(data)와 레이아웃 요소(layout)을 명시적으로 입력하는 방법

fig = go.Figure(
    data=[
        go.Scatter(
            x=df['x1'],
            y=df['x2'],
            mode="markers",
        )
    ],
    layout=go.Layout(
        title="QH",
        xaxis=dict(title="x", range=(0,5000)),
        yaxis=dict(title="y", range=(7,8)),
    ),
)
fig.update_xaxes(minor=dict(ticklen=5, tickcolor="black", showgrid=True, gridcolor='white', griddash='dash'), minor_griddash="dot")
fig.update_yaxes(minor_ticks="inside")
fig.update_layout(autosize=False, width=1200, height=600)
fig.show()

1-2. go.Figure에 add_trace()과 update_layout()을 개별적으로 입력하는 방법

fig = go.Figure()

fig.add_trace(
    go.Scatter(x=df['x1'], y=df['x2'], mode="markers")
)

fig.update_layout(
    title="QH",
    autosize=False,
    width=1200,
    height=600,
    xaxis=dict(
        title="x",
        range=(0,5000),
        minor=dict(ticklen=5, tickcolor="black", showgrid=True, gridcolor='white', griddash='dash'),
        minor_griddash="dot"
    ),
    yaxis=dict(
        title="y",
        range=(7,8),
        minor_ticks="inside"
    ),
)

fig.show()

1-3. make_subplots()를 사용해서 Figure를 분할하는 방법

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

fig.add_trace(
    go.Scatter(x=df['x1'], y=df['x2'], mode="markers")
)

fig.update_layout(
    title="QH",
    autosize=False,
    width=1200,
    height=600,
    xaxis=dict(
        title="x",
        range=(0,5000),
        minor=dict(ticklen=5, tickcolor="black", showgrid=True, gridcolor='white', griddash='dash'),
        minor_griddash="dot"
    ),
    yaxis=dict(
        title="y",
        range=(7,8),
        minor_ticks="inside"
    ),
)

fig.show()

이렇게 하면, 1-1, 1-2와 동일한 챠트가 생성된다. (기본적으로 1X1)
만약 (rows=2, cols=1)로 설정하면, 자동적으로 첫번째로 add된 graph가 (1,1)자리에 위치하여, y축의 크기가 절반으로 줄어드는 것을 확인할 수 있다.

fig.add_trace(
    go.Scatter(x=df['x1'], y=df['x2'], mode="markers", row=1, col=1)
)

row=1, col=1을 go.Scatter 객체 생성 시 추가해주면, 명시적으로 Subplots의 위치를 지정할 수 있다. (물론, 2X1로 fig를 생성해놓고, col을 1이 아닌 다른 값을 넣으면 'IndexError' 에러가 발생한다.)

# fig = go.Figure()
fig = make_subplots(
    rows=2, cols=2,
    specs=[[{'rowspan': 2},{}],[None,{}]],
    horizontal_spacing=0.03,
    column_widths=[0.3, 0.7],
)

fig.add_trace(
    go.Scatter(x=df['x1'], y=df['x2'], mode="markers", name="QH"),
    row=1,col=1
)

fig.add_trace(
    go.Scatter(x=df['ts'], y=df['x1'], mode='lines', name="FR"),
    row=1,col=2,
)

fig.add_trace(
    go.Scatter(x=df['ts'], y=df['x3'], mode='lines', name="LE"),
    row=2,col=2,
)

fig.update_layout(
    title="QH",
    autosize=False,
    width=1800,
    height=800,
    xaxis=dict(
        title="x",
        range=(0,5000),
        minor=dict(ticklen=5, tickcolor="black", showgrid=True, gridcolor='white', griddash='dash'),
        minor_griddash="dot"
    ),
    yaxis=dict(
        title="y",
        range=(7.1,7.5),
        minor_ticks="inside"
    ),
)

fig.show()

specs를 통해 2행 2열의 subplot을 병합(merge)시키는 설정을 할 수도 있음. (위 예시는 1행 1열의 subplot의 행을 2개 사용, 2행 1열은 그래프 없음. 으로 설정해서 병합시키는 예제)

horizontal_spacing을 통해 '열' 간 여유공간을 늘리고 줄일 수 있다.

column_widths를 통해 '열' 간 길이 비율을 조절할 수 있다.

add_trace의 row, col을 지정해서 각 자리의 subplot을 추가로 설정했다.

2. Dash를 사용해서 좀 더 인터렉티브한 그래프를 만들기

fig = make_subplots(
    rows=2, cols=2,
    specs=[[{'rowspan': 2},{}],[None,{}]],
    horizontal_spacing=0.03,
    column_widths=[0.3, 0.7],
)

fig.add_trace(
    go.Scatter(x=df['x1'], y=df['x2'], mode="markers", name="QH"),
    row=1,col=1
)

fig.add_trace(
    go.Scatter(x=df['ts'], y=df['x1'], mode='lines', name="FR"),
    row=1,col=2,
)

fig.add_trace(
    go.Scatter(x=df['ts'], y=df['x3'], mode='lines', name="LE"),
    row=2,col=2,
)

fig.update_layout(
    title="QH",
    autosize=False,
    width=1800,
    height=800,
    xaxis=dict(
        title="x",
        range=(0,5000),
        minor=dict(ticklen=5, tickcolor="black", showgrid=True, gridcolor='white', griddash='dash'),
        minor_griddash="dot"
    ),
    yaxis=dict(
        title="y",
        range=(7.1,7.5),
        minor_ticks="inside"
    ),
)

fig.show()
profile
AI 기술로 먹고 살기

0개의 댓글

Powered by GraphCDN, the GraphQL CDN