๋ฌ๋์คํผ ์์ ์ ๋ฆฌ
์์นํ x ์์นํ : scatterplot, lmplot, jointplot
์์นํ x ์นดํ ๊ณ ๋ฆฌํ : boxplot, violinplot, barplot, heatmap
์์นํ x ์์นํ : folium ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ฉ
๋ณธ ํฌ์คํ ์์๋ seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ distplot, relplot, jointplot, pairplot, boxplot, swarmplot, heatmap ์ ์ฌ์ฉํ๋ค.
๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ & ์ดํด๋ณด๊ธฐ
import seaborn as sns
seaborn ์ import ํด์ฃผ๊ณ , ์ฝ์ด๋ sns๋ฅผ ์ด๋ค.
raw = sns.load_dataset('tips')
seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ์ฅ๋ ๋ฐ์ดํฐ์ ์ ๋ถ๋ฌ์ฌ ์ ์๋ค. ๋ฐ์ดํฐ๋ ๋ฐ์ดํฐํ๋ ์ ํ์์ผ๋ก, pandas๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ์ํ ์ ์๋ค.
raw.head()
total_bill ( ์ด ๊ธ์ก ) , tip ( ํ ) , sex ( ์ฑ๋ณ ) , smoker ( ํก์ฐ ์ฌ๋ถ ) , day ( ์์ผ ) , time ( ์์ฌ ์๊ฐ ) , size ( ์ธ์ ์ )
raw.info()
info()๋ฅผ ์ด์ฉํ์ฌ row, column์ ๊ฐ์, data type, ๊ฒฐ์ธก๊ฐ ํ์ธ์ ํด ์ค๋ค.
๋ฐ์ดํฐ ๋ถํฌ ์ดํด๋ณด๊ธฐ (์์นํ)
- sns.distplot( df[ '์ปฌ๋ผ๋ช ' ] )
raw['total_bill']
์์นํ ์๋ฃ์ธ total_bill ์ปฌ๋ผ์ ๋ถํฌ๋ฅผ ์ดํด๋ณธ๋ค.
sns.distplot(raw['total_bill'])
๋ฐ์ดํฐ ๋ถํฌ ์ดํด๋ณด๊ธฐ ( ์์นํ vs ์์นํ )
- relplot( data=df, x=, y=, hue=, kind='scatter' )
: ๋ ๊ฐ์ ์์นํ ๋ณ์ ๋ถํฌ ํ์ธ
- kind = 'scatter' (default)
- kind = 'line'
sns.relplot(x = 'tip', y = 'total_bill', data = raw) # kind ์ต์
๊ฐ ๋ฏธ์ง์ ์ "scatter"
sns.relplot(x = 'tip', y = 'total_bill', data = raw, kind = 'line')
sns.relplot(x = 'tip', y = 'total_bill', data = raw, hue = 'sex')
hue๋ฅผ ์ฑ๋ณ๋ก ์ง์ ํ์ฌ ์ฑ๋ณ์ ๊ธฐ์ค์ผ๋ก ๋๋์ด ํ์ธํด๋ณผ ์ ์๋ค.
๊ด๊ณ ์ดํด๋ณด๊ธฐ (์์นํ vs ์์นํ )
- jointplot ( data=df, x=, y=, kind='scatter' )
- kind = 'scatter' : (default / point)
- kind = 'reg' : (point + regression)
- kind = 'kde' : ๋์ ๋ถํฌ์ฐจํธ like ์ง๋
sns.jointplot(data = raw, x = 'tip', y = 'total_bill') # kind ๊ฐ ๋ฏธ ์ง์ ์ ๊ธฐ๋ณธ ๊ฐ์ kind = 'scatter'
sns.jointplot(data = raw, x = 'tip', y = 'total_bill', kind = 'kde')
sns.jointplot(data = raw, x = 'tip', y = 'total_bill', kind = 'reg')
sns.jointplot(data = raw, x = 'tip', y = 'total_bill', kind = 'hex')
kind='hex'๋ kde์ ๋น์ทํ๋ฐ, ๊ฒน์น์ง ์๋ ์ ์ก๊ฐํ์ผ๋ก ํ์๋๋ค.
- pairplot( data=df )
: df ์ ๋ชจ๋ ์์นํ๋ฐ์ดํฐ ์ปฌ๋ผ์์ ๋ ์ปฌ๋ผ์ฉ ๊ด๊ณ๋ฅผ ์๊ฐํ ํจ
sns.pairplot(data = raw)
์๋ก ๊ฐ์ ์ปฌ๋ผ์ด๋ฉด ํ์คํ ๊ทธ๋จ์ ๊ทธ๋ฆฌ๊ณ , ๋ค๋ฅธ ์ปฌ๋ผ์ ๋ ์์นํ ๋ฐ์ดํฐ์ ๊ด๊ณ๋ฅผ ์ฐ์ ๋๋ก ํ์ํด ์ค๋ค.
sns.pairplot(data = raw, hue = 'sex')
hue๋ฅผ ์ฑ๋ณ๋ก ์ง์ ํ์ฌ ์ฑ๋ณ ๊ฐ ๋ถํฌ๋ฅผ ํ์ธํ ์ ์๋ค.
๋ฐ์ดํฐ ๋ถํฌ ์ดํด๋ณด๊ธฐ (์์นํ vs ์นดํ๊ณ ๋ฆฌํ)
- boxplot( data = df, x = , y = , hue = )
sns.boxplot(data = raw, x = 'day', y = 'tip')
์์ผ ๋ณ ํ์ ๋ถํฌ๋ฅผ ํ์ธํ๋ค.
sns.boxplot(data = raw, x = 'day', y = 'tip', hue = 'smoker')
hue๋ฅผ smoker๋ก ์ง์ ํ์ฌ ํก์ฐ์ ์ฌ๋ถ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ถํฌ๋ฅผ ํ์ธํ ์ ์๋ค.
- swarmplot( data=, x=, y=, hue=, dodge= )
: boxplot๊ณผ ๋น์ทํ๋ค. boxplot์ ํ๋์ ๊ธฐ์ค์ ๋ํ ๋ฐ์ดํฐ ๋ถํฌ ๋ฒ์๋ฅผ ํ์ ํ๋ ๋ฐ ์ฉ์ดํ์ง๋ง , ๋ฐ์ดํฐ์ ๊ฐ์๋ฅผ ํํํ์ง๋ ์๊ธฐ ๋๋ฌธ์ , ๋ฐ์ดํฐ ๊ฐ์๊ฐ ๋ค๋ฅธ ๊ฐ์ ๋น๊ตํ๊ธฐ์๋ ๋ฌธ์ ๊ฐ ์์ ์ ์๋ค. ์ด ๋ swarmplot์ ์ด์ฉํ๋ค.
sns.swarmplot(data = raw, x = 'day', y = 'tip', hue = 'smoker', dodge=True)
dodge=False๋ก ํ๋ฉด ๋ ์์ด ๊ฒน์ณ ๋์จ๋ค.
sns.boxplot(data = raw, x = 'day', y = 'tip', hue = 'smoker')
sns.swarmplot(data = raw, x = 'day', y = 'tip', hue = 'smoker', dodge=True)
boxplot๊ณผ swarplot์ ๊ฒน์ณ์ ๊ทธ๋ ค๋ณผ ์๋ ์๋ค.
sns.boxplot(data = raw, x = 'size', y = 'tip', hue = 'sex')
- barplot( data=df, x=, y=, hue= )
sns.barplot(data = raw, x = 'size', y = 'tip', hue = 'sex')
๋ฐ์ดํฐ ์ดํด๋ณด๊ธฐ (์์นํ vs ์นดํ ๊ณ ๋ฆฌํ vs ์นดํ ๊ณ ๋ฆฌํ)
- heatmap( data = df )
df = raw.pivot_table(index = 'day', columns = 'size', values = 'tip', aggfunc='mean')
df
์ฐ์ ์ธ๋ฑ์ค๋ฅผ day, column์ size, value๋ฅผ tip์ผ๋ก ์ค์ ํ์ฌ ์์ผ ๋ณ ์ธ์์์ ๋ฐ๋ฅธ tip ํ ์ด๋ธ์ ์์ฑํ๋ค.
sns.heatmap(data = df)
์นดํ ๊ณ ๋ฆฌํ - ์นดํ ๊ณ ๋ฆฌํ์ ๊ด๊ณ๋ฅผ ์์นํ์ ์ด์ฉํ์ฌ ํํํ๋ heatmap์ด๋ค.
๋ค์๊ณผ ๊ฐ์ ์ฌ๋ฌ ์ค์ ์ ํ ์ ์๋ค.
sns.heatmap(data = df,
annot = True, fmt = '.2f')
sns.heatmap(data = df,
annot = True, fmt = '0.2f',
cmap = 'Pastel1')
* annot : ์ซ์ ํ์, fmt : ์ซ์ ํฌ๋งคํ , cmap : ์๊น
* cmap ์ถ์ฒ ์ : Reds , Blues, Vlag, Pastel1, RdBu_r