[Python.Seaborn] Predefined Plots 5 - Pair Plot과 LM Plot

2021. 2. 21. 05:00Python과 머신러닝/MatPlotLib 데이터 시각화

0. 이전 포스트

 

1. Pair Plot - 데이터 간의 상관관계 표시

In [36]:iris = sns.load_dataset('iris') 
        g = sns.PairGrid(iris) 
        g.map(plt.scatter)

Out[36]

  • PairPlot 혹은 PairGrid한 dataset에 존재하는 모든 series들 간의 상관관계를 한 번에 보여주는 표이다.
  • 각 X변수들 간에 어떤 관계가 있는지 보는 것도 데이터 분석에 큰 도움이 된다.
  • 위와 같이 기본으로 설정할 경우, diagonal에 무의미한 분포도가 나오는데, 여기에 특정 표를 지정할 수도 있다.

 

2. PairPlot - Diagonal에 들어갈 그래프 지정하기

In [37]:g = sns.PairGrid(iris)
        g.map_diag(plt.hist)
        g.map_offdiag(plt.scatter)

 

Out[37]

  • g.map_diag(plt.hist) : diagonal 위치에는 histogram을 그려라
  • g.map_offdiag(plt.scatter) : diagonal 외에는 변수들 간의 scatter plot을 그려라
  • 이와 같이 원하는 위치에 원하는 형태의 그래프를 지정해서 X변수들 간의 상관관계를 그릴 수 있다.

 

3. PairPlot - Hue 별로 구분하기

In [38]:g = sns.PairGrid(iris, hue='species') 
        g.map_diag(plt.hist)
        g.map_offdiag(plt.scatter) 
        g.add_legend()

Out[38]

  • PairPlot도 타 seaborn plot들과 동일하게 hue별로 구분해서 상관관계를 볼 수 있다.
  • 이렇게 볼 경우 훨씬 더 의미 있는 상관관계를 파악할 수 있다.
  • g = sns.PairGrid(iris, hue='species') : species 별로 구분해서 PairGrid 그리기
  • g.add_legend() : 각 hue(species) 별 구분자를 볼 수 있도록 우측에 표시

 

 

4. LM Plot - Category 별 Regression Plot 그리기

In [39]:sns.lmplot(x='total_bill', y='tip', col='smoker', data=tips)

Out[39]

  • 기존의 Regression plot은 전체 데이터를 기반으로 regression을 했다면, LM Plot은 category 별로 데이터를 구분해서 regression을 수행한다.

 

5. LM Plot - with Hue

In [40]:g = sns.lmplot(x='size', 
                       y='total_bill',
                       hue='day',
                       col='day', 
                       data=tips, 
                       height=6, 
                       aspect=.4,
                       x_jitter=.1)

Out[40]

  • 위와 같이 동일한 hue/col별로 구분한다면, 각 Data별로 조금은 더 구별이 쉽게 표현해준다.
반응형