seaborn을 활용한 시각화_펭귄종류에 대한 신체정보 데이터셋
Seaborn이란?¶
matplotlib을 기본으로 다양한 시각화 기법을 제공하는 라이브러리. 엄청나게 화려한 시각화 기법들을 제공하며, 기본적으로 이쁩니다. histplot, kdeplot, jointplot, Facetgrid, ... pandas DataFrame과 매우 호환이 잘 됩니다. e.g. sns.xxxplot(data=df) <--- 기본세팅! https://seaborn.pydata.org/
# 라이브러리와 데이터를 불러오고, 시각화를 위한 세팅을 합니다.
import seaborn as sns
sns.set_theme(style='whitegrid')
penguins = sns.load_dataset("penguins")
penguins.tail()
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | |
---|---|---|---|---|---|---|---|
339 | Gentoo | Biscoe | NaN | NaN | NaN | NaN | NaN |
340 | Gentoo | Biscoe | 46.8 | 14.3 | 215.0 | 4850.0 | Female |
341 | Gentoo | Biscoe | 50.4 | 15.7 | 222.0 | 5750.0 | Male |
342 | Gentoo | Biscoe | 45.2 | 14.8 | 212.0 | 5200.0 | Female |
343 | Gentoo | Biscoe | 49.9 | 16.1 | 213.0 | 5400.0 | Male |
Histplot¶
가장 기본적으로 사용되는 히스토그램을 출력하는 plot. 전체 데이터를 특정 구간별 정보를 확인할 때 사용합니다.
# penguin 데이터에 histplot을 출력합니다.
sns.histplot(data=penguins, x="flipper_length_mm", hue="species", multiple='stack')
<AxesSubplot:xlabel='flipper_length_mm', ylabel='Count'>
Displot¶
distribution들을 여러 subplot들로 나눠서 출력해주는 plot. displot에 kind를 변경하는 것으로, histplot, kdeplot, ecdfplot 모두 출력이 가능합니다. e.g. displot(kind="hist")
### penguin 데이터에 displot을 출력합니다.
sns.displot(data=penguins, x="flipper_length_mm", hue="species", col="species")
<seaborn.axisgrid.FacetGrid at 0x18cbbc1fa60>
Barplot¶
어떤 데이터에 대한 값의 크기를 막대로 보여주는 plot. (a.k.a. 막대그래프) 가로 / 세로 두 가지로 모두 출력 가능합니다. 히스토그램과는 다릅니다!
# penguin 데이터에 barplot을 출력합니다.
sns.barplot(data=penguins, x="flipper_length_mm", y="species", hue='species')
#sns.barplot(data=penguins, y="flipper_length_mm", x="species", hue='species')
#sns.barplot(data=penguins, y="body_mass_g", x="species", hue='species')
<AxesSubplot:xlabel='flipper_length_mm', ylabel='species'>
Countplot¶
범주형 속성을 가지는 데이터들의 histogram을 보여주는 plot. 종류별 count를 보여주는 방법입니다.
# penguin 데이터에 countplot을 출력합니다.
sns.countplot(data=penguins, x='species', hue='sex')
<AxesSubplot:xlabel='species', ylabel='count'>
Boxplot¶
데이터의 각 종류별로 사분위 수(quantile)를 표시하는 plot. 특정 데이터의 전체적인 분포를 확인하기 좋은 시각화 기법입니다. box와 전체 range의 그림을 통해 outlier를 찾기 쉽습니다. (IQR : Inter-Quantile Range)
# penguin 데이터에 boxplot을 출력합니다.
#sns.boxplot(data=penguins, x="flipper_length_mm", y="species", hue="species")
#sns.boxplot(data=penguins, x="body_mass_g", y="species", hue="species")
#sns.boxplot(data=penguins, x="body_mass_g", y="species", hue="sex")
sns.boxplot(data=penguins, y="body_mass_g", x="species", hue="sex")
<AxesSubplot:xlabel='species', ylabel='body_mass_g'>
Violinplot¶
데이터에 대한 분포 자체를 보여주는 plot. boxplot과 비슷하지만, 전체 분포에 대한 그림을 보여준다는 점에서 boxplot과 다릅니다. 보통 boxplot과 함께 표시하면, 평균 근처에 데이터가 얼마나 있는지(boxplot) 전체적으로 어떻게 퍼져있는지(violinplot) 모두 확인이 가능합니다.
# penguin 데이터에 violinplot을 출력합니다.
#sns.violinplot(data=penguins, y="flipper_length_mm", x="species", hue="species")
sns.violinplot(data=penguins, y="body_mass_g", x="species", hue="sex")
<AxesSubplot:xlabel='species', ylabel='body_mass_g'>
Lineplot¶
특정 데이터를 x, y로 표시하여 관계를 확인할 수 있는 plot. (선 그래프) 수치형 지표들 간의 경향을 파악할 때 많이 사용합니다.
# penguin 데이터에 lineplot을 출력합니다.
#sns.lineplot(data=penguins, x="body_mass_g", y="flipper_length_mm", hue="species")
sns.lineplot(data=penguins, y="body_mass_g", x="flipper_length_mm", hue="sex")
#sns.lineplot(data=penguins, y="bill_length_mm", x="bill_depth_mm", hue="species")
<AxesSubplot:xlabel='flipper_length_mm', ylabel='body_mass_g'>
Pointplot¶
특정 수치 데이터를 error bar와 함께 출력해주는 plot. 수치 데이터를 다양한 각도에서 한 번에 바라보고 싶을 때 사용합니다. 데이터와 error bar를 한 번에 찍어주기 때문에, 살펴보고 싶은 특정 지표들만 사용하는 것이 좋습니다.
# penguin 데이터에 pointplot을 출력합니다.
#sns.pointplot(data=penguins, y="flipper_length_mm", x="sex", hue="species")
sns.pointplot(data=penguins, y="bill_length_mm", x="sex", hue="species")
<AxesSubplot:xlabel='sex', ylabel='bill_length_mm'>
Scatterplot¶
lineplot과 비슷하게 x, y에 대한 전체적인 분포를 확인하는 plot. lineplot은 경향성에 초점을 둔다면, scatterplot은 데이터 그 자체가 퍼져있는 모양에 중점을 둡니다
# penguin 데이터에 scatterplot을 출력합니다.
sns.scatterplot(data=penguins, x="body_mass_g", y="flipper_length_mm", hue="species")
#sns.scatterplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="sex")
<AxesSubplot:xlabel='body_mass_g', ylabel='flipper_length_mm'>
Pairplot¶
주어진 데이터의 각 feature들 사이의 관계를 표시하는 Plot. scatterplot, FacetGrid, kdeplot을 이용하여 feature간의 관계를 잘 보여줍니다. 각 feature에 대해 계산된 모든 결과를 보여주기 때문에, feature가 많은 경우 사용하기 적합하지 않습니다.
# penguin 데이터에 pairplot을 출력합니다.
sns.pairplot(data=penguins, hue="island")
<seaborn.axisgrid.PairGrid at 0x18cbe68b5b0>
sns.pairplot(data=penguins, hue="species")
<seaborn.axisgrid.PairGrid at 0x18cbe567880>
Heatmap¶
정사각형 그림에 데이터에 대한 정도 차이를 색 차이로 보여주는 plot. 말 그대로 heatmap이기 때문에, 열화상카메라로 사물을 찍은 것처럼 정보의 차이를 보여줍니다. pairplot과 비슷하게 feature간 관계를 시각화할 때 많이 사용합니다.
penguins.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 344 entries, 0 to 343
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 species 344 non-null object
1 island 344 non-null object
2 bill_length_mm 342 non-null float64
3 bill_depth_mm 342 non-null float64
4 flipper_length_mm 342 non-null float64
5 body_mass_g 342 non-null float64
6 sex 333 non-null object
dtypes: float64(4), object(3)
memory usage: 18.9+ KB
# 각 feature간 상관관계를 파악하기 위해 Correlation matrix를 만듭니다.
corr = penguins.corr()
corr
bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | |
---|---|---|---|---|
bill_length_mm | 1.000000 | -0.235053 | 0.656181 | 0.595110 |
bill_depth_mm | -0.235053 | 1.000000 | -0.583851 | -0.471916 |
flipper_length_mm | 0.656181 | -0.583851 | 1.000000 | 0.871202 |
body_mass_g | 0.595110 | -0.471916 | 0.871202 | 1.000000 |
# penguin 데이터에 heatmap을 출력합니다.
sns.heatmap(corr)
<AxesSubplot:>