코딩하는 해맑은 거북이

[데이터시각화] Interactive Visualization 본문

Data Analysis & Viz

[데이터시각화] Interactive Visualization

#CJE 2023. 3. 27.
본 게시물의 내용은 '부스트캠프 AI Tech - Data Visualization(안수빈)' 강의를 듣고 작성하였다.
해당 글은 아래의 3가지를 다룬다.
🍀 Interactive
🍀 Interactive 시각화 라이브러리
🍀 Plotly Express 실습 (🚩Iris, flights, medals)

 

🍀 Interactive

🟡 정적 시각화

정형 데이터의 각각의 관계를 살펴보는데 feature가 많아지면 공간적 낭비가 심해진다.

그리고 각각의 사용자가 원하는 인사이트가 다를 수 있고, 필요한 인터랙션을 통해 원하는 정보를 추가해줄 수 있다.

물론 설득을 위해선 원하는 메세지를 압축해서 담는 것은 정적 시각화를 사용하는 것이 나을 수 도 있다.

 

🟡 종류 : 참고자료

- Select : mark something as interesting

- Explore : show me something else

- Reconfigure : show me a different arrangement

- Encode : show me a different representation

- Abstract : show me more or less detail

- Filter : show me something conditionally

- Connect : show me related items

 

🟡 라이브러리 3가지

  • Plotly
  • Bokeh
  • Altair

 

 

🍀 Interactive 시각화 라이브러리

🟡 Matplotlib

- 주피터 노트북 환경 또는 Local에서만 실행할 수 있다는 단점이 있다

 

🟡 Plotly

- 인터랙티브 시각화에 가장 많이 사용되는 Plotly

- Python 뿐만이 아니라 R, JS에서도 제공해서 호환이 좋다

- 통계 시각화 외에도 지리 시각화 + 3D 시각화 + 금융 시각화 등 다양한 시각화 기능 제공

- 기본 color인 형광 Color가 인상적이다

 

🟡 Plotly Express : 참고자료

- Plotly를 seaborn과 유사하게 만들어 쉬운 문법

- 커스텀 부분이 부족하지만 다양한 함수 제공

 

🟡 Bokeh

- 문법은 Matplotlib과 더 유사한 부분이 있음

- 기본 Theme이 Plotly에 비해 깔끔하다.

- 그러나, plotly에 비해 문서화가 비교적 부족하다

 

🟡Altair

- Vega 라이브러리를 사용하여 만든 인터랙티브

- 시각화를 + 연산 등으로 배치하는 것이 특징

- 문법이 Pythonic하지 않아서 문법에 혼동을 줄 수 있다

- 데이터 크기에 5000개 제한되어 있어서 추천하진 않지만, 기본차트를 사용할 때 쓰면 좋다

- Bar, Line, Scatter, Histogram에 특화

 

 

 

🍀 Plotly Express 실습

- 라이브러리 선언

import plotly.express as px

 

🚩 Iris

iris = px.data.iris() 
iris.head()

 

 

- Scatter

  • x : x label
  • y : y label
  • size : 주어진 범주에 따른 점의 크기 설정
  • color : 주어진 범주를 색을 달리해서 구분
fig = px.scatter(iris, 
                 x='sepal_length',
                 y='petal_length',
                 size='sepal_length',
                 color='species',             
            )

fig.show()

  • range_x : x label의 범위 조정
  • range_y : y label의 범위 조정
fig = px.scatter(iris, 
                 x='sepal_length',
                 y='petal_length',

                 range_x=[4, 8],    # 범위 조정
                 range_y=[0, 7],
)

fig.show()

  • marginal_x : 2개의 feature의 결합확률 분포를 x label 위에 설정
  • marginal_y : 2개의 feature의 결합확률 분포를 y label 옆에 설정
fig = px.scatter(iris, 
                 x='sepal_length',
                 y='petal_length',
                 color='species',
                
                 marginal_x="box", 
                 marginal_y="violin",
)

fig.show()

  • hover_data : 각 데이터에 대한 추가적인 정보를 넣을 수 있다
  • hover_name : 각 데이터에 대한 정보의 이름을 설정한다
fig = px.scatter(iris, 
                 x='sepal_length',
                 y='petal_length',
                 color='species',

                 hover_data=['sepal_width', 'petal_width'],
                 hover_name='species'
            )

fig.show()

  • trendline : 회귀선을 그려준다
fig = px.scatter(iris, 
                 x='sepal_length',
                 y='petal_length',
                 color='species',
                 trendline="ols",
            )

fig.show()

  • facet_col : 열로 분할
  • facet_row : 행으로 분할
fig = px.scatter(iris, 
                 x='sepal_length',
                 y='petal_length',
                 color='species',
                 facet_row='species',
            )

fig.show()

fig = px.scatter(iris, 
                 x='sepal_length',
                 y='petal_length',
                 color='species',
                 facet_col='species',
            )

fig.show()

 

 

- Line

🚩 flights

flights = sns.load_dataset('flights')
flights.head()

fig = px.line(flights, 
              x='year',
              y='passengers',
              color='month',)

fig.show()

 

 

- Bar

🚩 medals

medals = px.data.medals_long()
medals

fig = px.bar(medals, 
             x="nation", 
             y="count", 
             color="medal")

fig.show()

medals_wide = px.data.medals_wide()
medals_wide

fig = px.bar(medals_wide, 
             x="nation", 
             y=["gold", "silver", "bronze"], )

fig.show()

fig = px.bar(medals, 
             x="nation", 
             y="count", 
             color="medal",
             barmode="group",)

fig.show()

 

Comments