[Python] Seaborn 기본


Seaborn의 기본 내용 정리한 내용임

import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore") # 경고메세지 무시
# 펭귄데이터
penguins = sns.load_dataset("penguins")
penguins.info()
penguins.head()
<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
speciesislandbill_length_mmbill_depth_mmflipper_length_mmbody_mass_gsex
0AdelieTorgersen39.118.7181.03750.0Male
1AdelieTorgersen39.517.4186.03800.0Female
2AdelieTorgersen40.318.0195.03250.0Female
3AdelieTorgersenNaNNaNNaNNaNNaN
4AdelieTorgersen36.719.3193.03450.0Female
#히스토그램
sns.histplot(data=penguins, x="flipper_length_mm", hue="species", multiple="stack")
<AxesSubplot:xlabel='flipper_length_mm', ylabel='Count'>

img

# kernel density estimation
sns.kdeplot(data=penguins, x="flipper_length_mm", hue="species", multiple="stack")
<AxesSubplot:xlabel='flipper_length_mm', ylabel='Density'>

img

Figure Level vs Axes Level Functions

  1. axes-level는 matplotlib.pyplot.axes를 기준으로 만들어지고
  2. Figure-level은 FacetGrid를 기준으로 만들어진다. img

Distribution Plots

distplot

  • 분포 확인
  • kde차트와 히스토그램 확인가능
sns.distplot(penguins["flipper_length_mm"], bins=40)
# kde=False를 하면 kde차트는 사라짐
C:\Users\Jessie\anaconda3\lib\site-packages\seaborn\distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)





<AxesSubplot:xlabel='flipper_length_mm', ylabel='Density'>

img

jointplot

  • Scatterplot을 기본으로 각 축의 분포 확인 가능
sns.jointplot(x="flipper_length_mm",y="bill_length_mm",data=penguins, hue="species")
# hue="species" - 색반환
# kind="hex" - 육각형 모양으로 반환
# kind="reg" - Regression plot
# kind="kde"  - 등고선
<seaborn.axisgrid.JointGrid at 0x165bc527550>

img

sns.jointplot(x="flipper_length_mm",y="bill_length_mm",data=penguins, kind="reg" )
<seaborn.axisgrid.JointGrid at 0x165bc4fe6d0>

img

pairplot

  • 모든 Numeric 변수에 대하여 Scatter plot과 분포도 그림
sns.pairplot(data=penguins, hue="species", palette="coolwarm")
<seaborn.axisgrid.PairGrid at 0x165bf0f27c0>

img

rugplot

sns.rugplot(penguins["flipper_length_mm"])
<AxesSubplot:xlabel='flipper_length_mm'>

img

Categoricla Plots

barplot

  • estimator인자는 Barplot의 y축을 계량하는 기준을 의미하며 default값은 mean이다.
import numpy as np
sns.barplot(data=penguins, x="species", y="flipper_length_mm", estimator=np.std) # 표준편차
<AxesSubplot:xlabel='species', ylabel='flipper_length_mm'>

img

countplot

sns.countplot(data=penguins, x="species")
<AxesSubplot:xlabel='species', ylabel='count'>

img

boxplot

sns.boxplot(x="species",y="bill_length_mm",data=penguins, hue="sex")
<AxesSubplot:xlabel='species', ylabel='bill_length_mm'>

img

viloin plot

sns.violinplot(x="species",y="bill_length_mm",data=penguins, hue="sex", split=True)
<AxesSubplot:xlabel='species', ylabel='bill_length_mm'>

img

stripplot

sns.stripplot(x="species",y="bill_length_mm",data=penguins, jitter=True, hue="sex", split=True)
C:\Users\Jessie\anaconda3\lib\site-packages\seaborn\categorical.py:2805: UserWarning: The `split` parameter has been renamed to `dodge`.
  warnings.warn(msg, UserWarning)





<AxesSubplot:xlabel='species', ylabel='bill_length_mm'>

img

swarmplot

  • stripplot과 violing plot의 조합으로 볼 수 있음
sns.swarmplot(x="species",y="bill_length_mm",data=penguins)
<AxesSubplot:xlabel='species', ylabel='bill_length_mm'>

img

Matrix chart

tc = penguins.corr()

Heatmap

  • annot=Ture 인자를 통해서 히트맵에 해당하는 셀의 값을 노출할 수 있다.
  • cmap을 통해 컬러맵 부여 가능
sns.heatmap(tc,annot=True, cmap="coolwarm")
<AxesSubplot:>

img

test = penguins.pivot_table(index="species", columns="island", values="bill_length_mm")
sns.heatmap(test, cmap="magma")
<AxesSubplot:xlabel='island', ylabel='species'>

img

Clustermap

sns.clustermap(tc, cmap="coolwarm", standard_scale=1, annot=True)
<seaborn.matrix.ClusterGrid at 0x165c2a16dc0>

img

Grid

iris = sns.load_dataset("iris")
iris.head()
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

PairGrid

g = sns.PairGrid(iris)
# g.map(plt.scatter) # scatter
g.map_diag(sns.distplot) # 사선에는 distplot
g.map_upper(plt.scatter) # 사선 상단에는 scatterplot
g.map_lower(sns.kdeplot) # 사선 아래에는 kdeplot
<seaborn.axisgrid.PairGrid at 0x165c7fc8790>

img

FacetGrid

  • Categorical한 변수를 기준으로 그래프를 쪼개서 볼 수 있음
  • Trellis(격자구조)의 개념이라고 생각하면 좋음
tips = sns.load_dataset("tips")
tips.head()
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
g = sns.FacetGrid(data=tips, col="time", row="smoker")
# g.map(sns.distplot, "total_bill")
g.map(plt.scatter, "total_bill", "tip")
<seaborn.axisgrid.FacetGrid at 0x165ca168850>

img

regplot

lmplot

sns.lmplot(x="total_bill", y="tip",data=tips, hue="sex", markers=['o','v'])
<seaborn.axisgrid.FacetGrid at 0x165ca196df0>

img

sns.lmplot(x="total_bill", y="tip",data=tips,col="sex",row="time") # auto FacetGrid
<seaborn.axisgrid.FacetGrid at 0x165ca32e400>

img





© 2021.04. by Jessie

Powered by jessie