차밍이

[Python] Plotly 그래프 사용법 - Line Plot 본문

파이썬/데이터 시각화

[Python] Plotly 그래프 사용법 - Line Plot

2021. 1. 28. 21:49
반응형

Plotly를 사용해서 Line plot을 그려보겠습니다.

1. Plotly Express - Lineplot

express 객체를 통해서 lineplot을 그릴 수 있습니다.

plotly에 있는 기본 데이터를 가져와서 그려보겠습니다.

import plotly.express as px

df = px.data.gapminder()
df.head()

Canada에 해당되는 부분만 선택해서 그래프를 그려보겠습니다.

canada = df[df['country']=='Canada'] # 케나다 만 선택

fig = px.line(canada, 
              x="year", # x축
              y="lifeExp",  # y축
              title='Life expectancy in Canada' # Title 
              )
fig.show()

대륙별로 색깔을 다르게 지정하고, line을 국가별로 선택하는 옵션을 넣었습니다.

fig = px.line(df, x="year", y="lifeExp", 
             title='Life expectancy in Canada',
             color='continent',
             line_group='country',
             hover_name='country')

fig.show()

2. Plotly - graph_objects - Scatter plot Line plot

graph_objects를 통해서 line plot을 그릴 때는 주로 Scatter를 사용해서 그리게 됩니다.

Scatter 산점도를 사용하되, mode를 line or marker를 선택해서 line그래프를 그립니다.

marker를 통해서 값을 좀 더 자세하게 볼 수도 있습니다.

import plotly.graph_objects as go

# Create random data with numpy
import numpy as np
np.random.seed(1)

N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N) + 5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5

# Create traces
fig = go.Figure()
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
                    mode='lines', # Line plot만 그리기
                    name='lines'))
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
                    mode='lines+markers', # Line Plot에 마커찍기
                    name='lines+markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y2,
                    mode='markers', # 마커만 찍기
                    name='markers'))

fig.show()

3. Plotly - graph_objects - Scatter & Line plot - 옵션

좀 더 상세하게 옵션을 지정해줄 수 있습니다.

plotly는 전체적으로 JSON 형식을 사용하기에 dictionary 형태로 옵션을 설정해주는 경우가 많습니다.

line plot을 그리는데 상세한 옵션을 선택해줄 수 있습니다.
line이라는 변수에 dict형태로 어떻게 라인을 설정할 것이지 지정해줍니다.
색깔이나 두께 혹은 그리는 방식(dash or dot) 등으로 표현할 수 있습니다.

import plotly.graph_objects as go

# 데이터를 생성하는 부분 List로 데이터를 생성
month = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
         'August', 'September', 'October', 'November', 'December']
high_2000 = [32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3]
low_2000 = [13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9]
high_2007 = [36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0]
low_2007 = [23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6]
high_2014 = [28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9]
low_2014 = [12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1]

fig = go.Figure()
# Create and style traces
fig.add_trace(go.Scatter(x=month, y=high_2014, name='High 2014',
                         line=dict(color='firebrick', width=4))) # Line에 옵션 선택
fig.add_trace(go.Scatter(x=month, y=low_2014, name = 'Low 2014',
                         line=dict(color='royalblue', width=4)))
fig.add_trace(go.Scatter(x=month, y=high_2007, name='High 2007',
                         line=dict(color='firebrick', width=4,
                              dash='dash') # dash options include 'dash', 'dot', and 'dashdot'
))

fig.add_trace(go.Scatter(x=month, y=low_2007, name='Low 2007',
                         line = dict(color='royalblue', width=4, dash='dash')))
fig.add_trace(go.Scatter(x=month, y=high_2000, name='High 2000',
                         line = dict(color='firebrick', width=4, dash='dot')))
fig.add_trace(go.Scatter(x=month, y=low_2000, name='Low 2000',
                         line=dict(color='royalblue', width=4, dash='dot')))

# Edit the layout
fig.update_layout(title='Average High and Low Temperatures in New York',
                   xaxis_title='Month',
                   yaxis_title='Temperature (degrees F)')


fig.show()

 

반응형

관련된 글 보기

Comments