본문 바로가기

Data visualization with python

[Python] seaborn 데이터시각화 graph 종류 총 정리

안녕하세요 :)
seaborn으로 데이터시각화하기 첫번째 게시물은 graph 나열로 시작합니다!
향후 그래프 하나하나의 사용법에 대하여 더 자세히 알아보도록 할게요~*
궁금한점이나 문의사항은 댓글로 남겨주세요!

 

20210104_seaborn_1

Chapter1. Python seaborn(sns)으로 만들 수 있는 그래프의 종류

seaborn 은 matplotlib와 함께 가장 기본적이고 강력한 시각화 툴로 손꼽힌다.

  1. seaborn은 문법이 더 쉽고 통계 시각화에 다양성을 가짐.
  2. seaborn은 여러 그림을 한번에 나타내어 편하지만 때문에 메모리를 많이 쓸 수 있음.
  3. matplotlib은 파이썬 전체적으로 사용가능하다면 seaborn은 pandas와 함께 사용하기에 더 직관적으로 특화되어있음.
  4. seaborn은 초보자가 사용하기 좋은 옵션으로 자동 세팅됨.
In [1]:
# 기본 라이브러리 설치
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
print('seaborn version: ',sns.__version__)

# 기본 데이터셋 불러오기(seaborn에서 기본 제공하는 데이터셋)
tips = sns.load_dataset('tips')
tips.head()
seaborn version:  0.11.1
Out[1]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

기본 그래프의 틀

In [2]:
# sns.~plot(data=tips, x=~, y=~) 형태
sns.barplot(data=tips, x='sex', y='tip')
Out[2]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef6c435370>
In [3]:
# sns.~plot(data=tips, x=~, y=~, hue=~) 형태 : 'hue' 옵션에 categorical variable(범주형변수)를 넣어 나눠 표현함
sns.barplot(data=tips, x='sex', y='tip',hue='smoker')
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef6f73e160>

여러개의 그래프를 한번에 (Subplots)

In [4]:
# 기본 데이터셋 불러오기(seaborn에서 기본 제공하는 데이터셋)
titanic = sns.load_dataset('titanic')
titanic.head()
Out[4]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True
In [5]:
# 3개의 그래프를 한번에 띄워보자 -> 1행 3열의 형태로 ax도 배열해야!
figure, (ax1, ax2, ax3)=plt.subplots(nrows=1, ncols=3)
# 크기는 subplot갯수에 맞게 조절
figure.set_size_inches(18,8)

# 'ax' 옵션을 주지 않는다면 오른쪽(ax3)부터 채워지게 된다.
sns.countplot(data=titanic, x='pclass', hue='survived', ax=ax1)
sns.countplot(data=titanic, x='sex', hue='survived', ax=ax2)
sns.countplot(data=titanic, x='embarked', hue='survived', ax=ax3)
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef6f8ec820>
In [6]:
# 5개의 그래프를 한번에 띄워보자 -> 2행 3열의 형태로 ax도 배열해야!
figure, ((ax1, ax2, ax3), (ax4, ax5, ax6))=plt.subplots(nrows=2, ncols=3)
# 크기는 subplot갯수에 맞게 조절
figure.set_size_inches(18,8)

# 'ax' 옵션을 주지 않는다면 오른쪽(ax3)부터 채워지게 된다.
sns.countplot(data=titanic, x='pclass', hue='survived', ax=ax1)
sns.countplot(data=titanic, x='sex', hue='survived', ax=ax2)
sns.countplot(data=titanic, x='embarked', hue='survived', ax=ax3)
sns.countplot(data=titanic, x='sibsp', hue='survived', ax=ax4)
sns.countplot(data=titanic, x='parch', hue='survived', ax=ax5)
Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef70185460>

seaborn 의 API 소개

1. Relational plots

  • relplot하나로 scatterplot 및 lineplot 커버 가능

1) scatterplot

  • Draw a scatter plot with possibility of several semantic groupings.
In [7]:
sns.scatterplot(data=tips, x="total_bill", y="tip")
Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef70276a30>

2) lineplot

  • Draw a line plot with possibility of several semantic groupings.
In [8]:
flights = sns.load_dataset("flights")
sns.lineplot(data=flights, x="year", y="passengers")
Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef705745b0>

2. Distribution plots

  • displot하나로도 histplot, kdeplot, ecdfplot, rugplot 커버 가능
  • 향후 distplot은 displot과 histplot으로 대체될 예정

1) histplot

  • Plot univariate or bivariate histograms to show distributions of datasets.
In [9]:
penguins = sns.load_dataset("penguins")
sns.histplot(data=penguins, x="flipper_length_mm")
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef7060c940>

2) kdeplot

  • Plot univariate or bivariate distributions using kernel density estimation.
In [10]:
sns.kdeplot(data=tips, x="total_bill")
Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef7060c9d0>

3) ecdfplot

  • Plot empirical cumulative distribution functions.
In [11]:
sns.ecdfplot(data=penguins, x="flipper_length_mm")
Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef7070e490>

4) rugplot

  • Plot marginal distributions by drawing ticks along the x and y axes.
In [12]:
sns.rugplot(data=tips, x="total_bill")
Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef70965820>

3. Categorical plots

  • catplot하나로도 커버 가능

1) countplot

  • Show the counts of observations in each categorical bin using bars.
In [13]:
titanic = sns.load_dataset("titanic")
sns.countplot(x="class", data=titanic)
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef6f80ba90>

2) barplot

  • Show point estimates and confidence intervals as rectangular bars.
In [14]:
sns.barplot(x="day", y="total_bill", data=tips)
Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef702f5280>

3) boxplot

  • Draw a box plot to show distributions with respect to categories.
In [15]:
sns.boxplot(x=tips["total_bill"])
Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef6fc11fd0>

4) violinplot

  • Draw a combination of boxplot and kernel density estimate.
In [16]:
sns.violinplot(x=tips["total_bill"])
Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef6fc6c580>

5) swarmplot

  • Draw a categorical scatterplot with non-overlapping points.
  • stripplot은 데이터 포인트가 중복되어 범주별 분포를 그리는 반면, swarmplot은 데이터의 분산까지 고려하여 데이터 포인트가 서로 중복되지 않도록 그림.
    • 데이터가 퍼져 있는 정도를 입체적으로 볼 수 있다.
In [17]:
sns.swarmplot(x=tips["total_bill"])
Out[17]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef6fd394f0>

3. Regression plots

1) lmplot

  • Plot data and regression model fits across a FacetGrid.
In [18]:
sns.lmplot(x="total_bill", y="tip", data=tips)
Out[18]:
<seaborn.axisgrid.FacetGrid at 0x7fef70777580>

2) displot

  • Plot data and a linear regression model fit.
  • The regplot() and lmplot() functions are closely related, but the former is an axes-level function while the latter is a figure-level function that combines regplot() and FacetGrid.
In [19]:
sns.regplot(x="total_bill", y="tip", data=tips, lowess=True)
Out[19]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef70e88730>

3) residplot

  • Plot the residuals of a linear regression.
In [20]:
iris = sns.load_dataset("iris") 
sns.residplot(x="petal_length", y="petal_width", data=iris, lowess=True) 
Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef7104bca0>

4. Matrix plots

1) heatmap

  • Plot rectangular data as a color-encoded matrix.
In [21]:
flights = sns.load_dataset("flights").pivot("month", "year", "passengers")
sns.heatmap(flights)
Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fef710b1250>

2) clustermap

  • Plot a matrix dataset as a hierarchically-clustered heatmap.
In [22]:
a=iris.head(20)
a.pop("species")
sns.clustermap(a)
Out[22]:
<seaborn.matrix.ClusterGrid at 0x7fef6fde3700>

5. Multi-plot grids

1) pairplot

  • Plot pairwise relationships in a dataset.
In [23]:
sns.pairplot(penguins)
Out[23]:
<seaborn.axisgrid.PairGrid at 0x7fef6fb8df70>

2) jointplot

  • Draw a plot of two variables with bivariate and univariate graphs.
In [24]:
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm")
Out[24]:
<seaborn.axisgrid.JointGrid at 0x7fef7213d0a0>