โน๏ธ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/ |
| Last Crawled | 2026-04-04 05:59:48 (4 days ago) |
| First Indexed | 2022-11-26 18:54:14 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ๊ธฐ์ดํธ - Snug Archive |
| Meta Description | ํ์ด์ฌ(Python)์๋ Matplotlib(๋งทํ๋กฏ๋ฆฝ), Plotly(ํ๋กํ๋ฆฌ), GGplot(์ง์งํ๋กฏ) ๋ฑ ๋ค์ํ ์๊ฐํโฆ |
| Meta Canonical | null |
| Boilerpipe Text | 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ
Last Updatedย ย
2024-09-10
Publishedย ย
2022-06-05
Python Seaborn
8๋ถ
๋ชฉ์ฐจ
Seaborn์ผ๋ก ์ผ๋ณ๋ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํ ํด๋ณด์
ํ์ด์ฌ(Python)์๋
Matplotlib(๋งทํ๋กฏ๋ฆฝ)
, Plotly(ํ๋กํ๋ฆฌ), GGplot(์ง์งํ๋กฏ) ๋ฑ ๋ค์ํ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์์ต๋๋ค. Matplotlib์ ์ ์ธ๊ณ์ ๋ฐ์ดํฐ ๊ณผํ์๋ค์ด ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. Plotly๋ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ plotly.js๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค์ด์ ธ, ๊ทธ๋ํ์ ํน์ ๋ถ๋ถ์ ํ๋/์ถ์ํ๊ฑฐ๋ ์ ์ฅํ๋ ๋ฑ ์น ์์์ ์ฌ์ฉ์์ ์ํธ์์ฉํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. GGplot์ R์ ggplot2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ฐํ์ผ๋ก ๊ฐ๋ฐ๋์ด, ๊ธฐ์กด์ R ์ฌ์ฉ์๋ค์ด ์ฌ์ฉํ๊ธฐ ํธ๋ฆฌํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
๊ทธ๋ ๋ค๋ฉด Seaborn์ด๋ ๋ฌด์์ผ๊น์? ๋, ๋ง์ ์๊ฐํ ๋๊ตฌ ์ค์์ Seaborn์ ์ฌ์ฉํ๋ฉด ์ข์ ์ด์ ๋ ๋ฌด์์ผ๊น์? Seaborn์ Matplotlib์ ์ฝ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด ๊ฐ๋ฐ๋ ๊ณ ์์ค(high-level) ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. Seaborn์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ ๊ฐ๊ฒฐํจ์
๋๋ค. Seaborn์ ์ด์ฉํ๋ฉด ๋น๊ต์ ์งง์ ์ฝ๋๋ก๋ ํต๊ณํ์ ์ฃผ์ ๊ทธ๋ํ๋ฅผ ๋น ๋ฅด๊ณ ํธ๋ฆฌํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. ๊ทธ๋ํ๋ฅผ ์ธ๋ถ ์ค์ ์์ด ๊ฐ๋จํ๊ณ ๋น ๋ฅด๊ฒ ๊ทธ๋ฆฌ๊ณ ์ถ๋ค๋ฉด Matplotlib๋ณด๋ค Seaborn์ ์ฌ์ฉํ๋ฉด ์ข์ต๋๋ค.
Seaborn์ ์ฌ์ฉ๋ฒ์ ๊ธฐ์ดํธ๊ณผ ์ฌํํธ์ผ๋ก ๋๋ฉ๋๋ค. ๊ธฐ์ดํธ์์๋ Seaborn์ ์ค์นํ๊ณ ์ค์ต์ ์ํ ๊ธฐ๋ณธ์ ์ธ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ๊ณผ ๋ณ์๊ฐ 1๊ฐ์ธ 1์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ๋ค๋ฃน๋๋ค.
ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ์ฌํํธ
์์๋ ๋ณ๋์ด 2๊ฐ ์ด์์ธ ๋ค์ฐจ์ ๊ทธ๋ํ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ๊ธฐ๋ณธํธ์์ ๋ค๋ฃฐ ์ ์ฒด ๊ทธ๋ํ์ ๊ฐ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Seaborn 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ ๋ก๋๋งตย
๊ทธ๋ผ Seaborn์ผ๋ก 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ๋ฅผ ํ๊ธฐ ์ ์ ์ค๋นํ ์ฌํญ๋ถํฐ ์์๋ณด๊ฒ ์ต๋๋ค.
์ค๋นํ๊ธฐ
์๋ด ์ฌํญ
Seaborn์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๊ธฐ ์ํด์๋ ๋ช ๊ฐ์ง ์ค๋น๊ฐ ํ์ํฉ๋๋ค. ์ฒซ์งธ, ์ค์ต ํ๊ฒฝ์
๋๋ค. ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค(Data Science)๋ฅผ ์ํ ํตํฉ๊ฐ๋ฐํ๊ฒฝ(IDE)์๋ ๋ํ์ ์ผ๋ก ์คํ์ด๋(Spyder), ์ํฐ(Atom), ํ์ด์ฐธ(PyCharm) ๋ฑ์ด ์์ต๋๋ค. ์ด ๊ธ์์๋ ์ฃผํผํฐ ๋
ธํธ๋ถ(Jupyter Notebook)์ ์ฌ์ฉํฉ๋๋ค. ์ฃผํผํฐ ๋
ธํธ๋ถ์ ์์ธํ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ์
์ฃผํผํฐ ๋
ธํธ๋ถ ํ๊ฒฝ ์ค์ ํ๊ธฐ
๋ฅผ ์ฐธ์กฐํด ์ฃผ์ธ์.
๋์งธ, ํต๊ณ ์ฉ์ด์ ๋ํ ๊ฐ๋จํ ์ดํด๊ฐ ํ์ํฉ๋๋ค. ๊ฐ ์ฉ์ด๋ ๊ฐ๋ตํ ์ค๋ช
ํ ์์ ์ด๋, ๊ฐ๋
์ ๋ํ ์์ธํ ์ค๋ช
์ด๋ ์์์ ๋ค๋ฃจ์ง ์์ต๋๋ค. ํต๊ณ ์ฉ์ด๋ฅผ ์ฐธ์กฐํ๋ฉด์ ๊ธ์ ์ฝ๊ณ ์ถ์ ๋ถ๋ค์
ํ๋ฅ ๊ณผ ํต๊ณ ๊ธฐ์ด ์ฉ์ด
๋ฅผ ํจ๊ป ์ฝ์ผ์๊ธฐ๋ฅผ ๊ถํด๋๋ฆฝ๋๋ค.
์
์งธ, Seaborn ํจ์์ ์ข
๋ฅ์
๋๋ค. Seaborn์ ์๊ฐํ ํจ์๋ ๊ทธ๋ํ ์์ค(figure-level)์ ํจ์์ ์ถ ์์ค(axes-level)์ ํจ์๋ก ๋๋ฉ๋๋ค. ๊ทธ๋ํ ์์ค์ ํจ์๋ ์์ ํจ์๋ก ๊ทธ๋ํ์ ์ข
๋ฅ๋ฅผ ์ง์ ํ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํด ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํฉ๋๋ค. ๋ฐ๋ฉด ์ถ ์์ค ํจ์๋ ๊ฐ ๊ทธ๋ํ์ ์ข
๋ฅ์ ํนํ๋ ํจ์์
๋๋ค. ์ถ ์์ค ํจ์๋ 1๊ฐ์ง ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฐ ๋ง์ถคํ๋์ด ์์ต๋๋ค.
์ด ๋ ์ข
๋ฅ์ ํจ์๋ฅผ ๊ตฌ๋ถํ๋ ๊ธฐ์ค์
Grid
์ ์์ฑ ์ฌ๋ถ์
๋๋ค.
displot()
,
catplot()
,
relplot()
ํจ์๋ ๋ชจ๋ ๊ทธ๋ํ ์์ค ํจ์์ด๋ฉฐ
seaborn.axisgrid.FacetGrid
๋ฅผ ๋ง๋ญ๋๋ค. ๋ฐ๋ฉด,
countplot()
,
hisplot()
,
striplot()
๋ฑ๊ณผ ๊ฐ์ ํจ์๋ ์ถ ์์ค ํจ์์ด๋ฉฐ ๊ฒฐ๊ณผ๋ก
AxesSubplot
์ ๋ง๋ญ๋๋ค.
FacetGrid
๋ ์ฌ๋ฌ ๊ทธ๋ํ๋ฅผ ํฌํจํ๋ ์์ ๊ทธ๋ํ๋ก,
FacetGrid
์์ ํน์ ํ์
AxesSubplot
๊ทธ๋ํ๋ง ์ถ์ถํด ์ํ๋ ์กฐ๊ฑด์ ์ ์ฉํ ์ ์์ต๋๋ค.
์ฌ์ฉํ๋ ์ต์
์ด ์๋ก ๋ค๋ฅธ ๊ฒฝ์ฐ๋ ์์ง๋ง ๋ณดํต ๊ทธ๋ํ ์์ค ํจ์์ ์ถ ์์ค ํจ์์ ์ต์
์ ์๋ก ํธํ๋ฉ๋๋ค. ๋ค๋ง, Matplotlib ๊ณผ์ ํธํ์ฑ์ด๋ ํ ๊ทธ๋ํ ์์ ๋ค๋ฅธ ๊ทธ๋ํ๋ฅผ ๊ฒน์ณ ๊ทธ๋ฆด ์ ์๋ค๋ ์ ์์๋ ์ถ ์์ค์ ํจ์๊ฐ ๊ทธ๋ํ ์์ค ํจ์๋ณด๋ค ์กฐ๊ธ ๋ ์ ์ฐํฉ๋๋ค. ์ฌ๊ธฐ์๋ ๊ทธ๋ํ ์์ค์ ํจ์๋ฅผ ์ค์ฌ์ผ๋ก ๊ทธ๋ํ๋ฅผ ์ดํด๋ณด๋, ๊ทธ๋ํ ์์ค์ผ๋ก ๊ทธ๋ฆด ์ ์๋ ๊ทธ๋ํ๋ ์ถ ์์ค ํจ์๋ก ๊ทธ๋ฆฌ๊ฒ ์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ์ง๊ธ๋ถํฐ Seaborn์ ์ค์นํด ๋ณด๊ฒ ์ต๋๋ค.
์ค์นํ๊ธฐ
1) ํ์ด์ฌ ๋ฐ pip ์ค์น ์ฌ๋ถ ํ์ธ
Seaborn์ ์ฌ์ฉํ๋ ค๋ฉด ํ์ด์ฌ๊ณผ ํ์ด์ฌ์ ํจํค์ง ๊ด๋ฆฌ ๋งค๋์ ์ธ
pip
์ด ํ์ํฉ๋๋ค. ์ฌ์ฉํ๊ณ ์๋ ์์คํ
์ ํ์ด์ฌ๊ณผ
pip
์ด ์ค์น๋์ด ์๋์ง ํ์ธํฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
python
-
V
pip
-
v
ํ์ด์ฌ๊ณผ
pip
์ด ์ ์ค์น๋์ด ์๋ค๋ฉด Seaborn์ ์ค์นํ ์ค๋น๊ฐ ๋ ๊ฒ์
๋๋ค. ๊ทธ๋ฌ๋ฉด ์ด์ Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๊ฒ ์ต๋๋ค.
2) ํจํค์ง ์ค์น
ํฐ๋ฏธ๋์
pip install
์ด๋ผ๋ ๋ช
๋ น์ด ๋ค์ ์ค์นํ๋ ค๋ ํจํค์ง์ ์ด๋ฆ์ธ
seaborn
์ ์
๋ ฅํฉ๋๋ค.
python
pip install seaborn
ํ์ด์ฌ/R ๋ฐฐํฌํ์ธ ์๋์ฝ๋ค(Anaconda)๋ก ์์
ํ์๋ ๋ถ๋ค์ ์๋์ ๊ฐ์ด
pip
๋ช
๋ น์ด ๋์
conda
๋ช
๋ น์ด๋ฅผ ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค.
python
conda install seaborn
3) ์ค์น ํ์ธ
์ค์น ๋ช
๋ น์ด๋ฅผ ์คํํ๋ค๋ฉด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์ ๋ก ์ ์ค์น๋์๋์ง ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค. ํจํค์ง์ ์ค์น ์ฌ๋ถ๋ฅผ ์๊ณ ์ถ๋ค๋ฉด ์ค์น๋ ํจํค์ง์ ๋ฒ์ ์ ๋ณด๋ฅผ ํ์ธํ๋ฉด ๋ฉ๋๋ค. ๋ค์ ์ฝ๋๋ฅผ ์
๋ ฅํด์ ์ค์น๋ Seaborn์ ๋ฒ์ ์ ๋ณด๊ฐ ๋ณด์ด๋ฉด Seaborn์ด ์ ์ค์น๋ ๊ฒ์
๋๋ค.
python
import
seaborn
as
sns
sns
.
__version__
๊ทธ๋ํ๋ฅผ ์ถ๋ ฅํด์ ์ค์น ์ฌ๋ถ๋ฅผ ํ์ธํ ์๋ ์์ต๋๋ค. ์ฝ๋๋ ์๋์ ๊ฐ์ต๋๋ค.
python
import
seaborn
as
sns
df
=
sns
.
load_dataset
(
'penguins'
)
sns
.
pairplot
(
df
,
hue
=
'species'
)
Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น๋ฅผ ์๋ฃํ๋ค๋ฉด ๋ค์์ ๊ธฐ๋ณธ์ ์ธ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
๊ธฐ๋ณธ ํ๊ฒฝ ์ค์
๊ธฐ๋ณธ ํ๊ฒฝ ์ค์ ์ ๊ทธ๋ํ ์ ์ญ์ ์ ์ฉ๋๋ ์คํ์ผ๋ง(styling)์
๋๋ค. ์ฝ๋๋ณ ํ๊ฒฝ ์ค์ ์ ๋ํ ์์ธํ ์ค๋ช
์ ํ์ด์ฌ Matplotlib ์ฌ์ฉ๋ฒ(์์ )์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
import
numpy
as
np
import
pandas
as
pd
import
matplotlib
.
pyplot
as
plt
from
matplotlib
import
rcParams
import
seaborn
as
sns
import
warnings
def
setting_styles_basic
(
)
:
rcParams
[
'font.family'
]
=
'Malgun Gothic'
rcParams
[
'axes.unicode_minus'
]
=
False
setting_styles_basic
(
)
warnings
.
filterwarnings
(
'ignore'
)
Matplotlib์ ์ด์ฉํ์ง ์๊ณ Seaborn์ผ๋ก ํ๊ฒฝ ์ค์ ์ ํ ์๋ ์์ต๋๋ค.
์ ์ฒด ์คํ์ผ๋ง
Seaborn์์ ๋ชจ๋ ์คํ์ผ๋ง์ ํ ๋ฒ์ ์ค์ ํ๋ ค๋ฉด
set_theme()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
set_theme: ๊ทธ๋ํ ์ ๋ฐ ๋ฐ ๋งค์ฒด๋ณ ์ค์ผ์ผ(scale), ์์ ํ๋ ํธ ์ง์
๋ค์ ํจ์๋
set_theme()
์ ์ผ์ ์ญํ ๋ถ๋ดํฉ๋๋ค.
set_style: ๊ทธ๋ํ ์ ๋ฐ ์คํ์ผ ์ง์
set_context: ๋งค์ฒด๋ณ ์ค์ผ์ผ ์ง์
set_palette: ์์ ํ๋ ํธ ์ง์
set_theme
set_theme()
ํจ์๋ ๊ทธ๋ํ ์ ๋ฐ์ ์ ์ฉ๋๋ ํ
๋ง(theme)๋ฅผ ์ง์ ํ๋ ํจ์์
๋๋ค.
set_theme()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๊ทธ๋ํ ์ ์ญ์ ์คํ์ผ๋ง์ ์ง์ ํ๋
set_style()
ํจ์์ ์ฌ์ฉํ ๋งค์ฒด์ ์ ํฉํ๋๋ก ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์กฐ์ ํ๋
set_context()
ํจ์๋ก ํ๋ ์ผ์ ํ ๋ฒ์ ํ ์ ์์ต๋๋ค. ์ฌ์ฉ ์์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
custom_params
=
{
"axes.spines.right"
:
False
,
"axes.spines.top"
:
False
}
sns
.
set_theme
(
context
=
'notebook'
,
style
=
'darkgrid'
,
palette
=
'deep'
,
font
=
'Malgun Gothic'
,
font_scale
=
1
,
rc
=
custom_params
)
context
context
ํ๋ผ๋ฏธํฐ๋ ์ฌ์ฉํ๋ ๋งค์ฒด์ ์ ํฉํ ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์กฐ์ ํ๋ ์ต์
์
๋๋ค. ์ด ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๊ฐ ๋งค์ฒด์ ์ ํฉํ๊ฒ ๋ผ๋ฒจ๊ณผ ๊ทธ๋ํ์ ํฌ๊ธฐ๋ฅผ ๋ง์ถค ์ค์ ํ ์ ์์ต๋๋ค. ์ ํํ ์ ์๋ ์ต์
์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
notebook: ๊ธฐ๋ณธ ์ค์
paper: ๋
ผ๋ฌธ, ๋ณด๊ณ ์
talk: ํ๋ฆฌ์ ํ
์ด์
poster: ํฌ์คํฐ
style
style
ํ๋ผ๋ฏธํฐ๋ Seaborn์ ๊ธฐ๋ณธ ๋ด์ฅ ํ
๋ง(built-in themes)๋ฅผ ์ง์ ํ๋ ์ต์
์
๋๋ค. ๋ด์ฅ ํ
๋ง์๋ ์ด 5๊ฐ์ง ํ
๋ง๊ฐ ์์ต๋๋ค.
darkgrid: ํ์ ๋ฐฐ๊ฒฝ์ + ๊ทธ๋ฆฌ๋
whitegrid: ํฐ์ ๋ฐฐ๊ฒฝ์ + ๊ทธ๋ฆฌ๋
dark: ํ์ ๋ฐฐ๊ฒฝ์
white: ํฐ์ ๋ฐฐ๊ฒฝ์
ticks: ํฐ์ ๋ฐฐ๊ฒฝ์ + ๋๊ธ
palette
palette
ํ๋ผ๋ฏธํฐ๋ ๊ทธ๋ํ์ ์์ ์ง์ ํ๋ ์ต์
์
๋๋ค. ์ฌ์ฉํ ์ ์๋ ๊ธฐ๋ณธ ์์ ํ๋ ํธ๋ ์ด 6๊ฐ์ง(
deep
,
muted
,
pastel
,
bright
,
dark
,
colorblind
)์
๋๋ค. ํน์ ํ๋ ํธ๋ฅผ ์ ํํ๋ ค๋ฉด
color_palette()
ํจ์๋ฅผ, ์ ํํ ํ๋ ํธ์ ์์์ ํ์ธํ๊ณ ์ถ๋ค๋ฉด
palplot()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
palette
=
sns
.
color_palette
(
'deep'
)
sns
.
palplot
(
palette
)
font, font_scale
font
์
font_scale
์ ๊ฐ๊ฐ ๊ธ๊ผด์ ์ข
๋ฅ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๋ ํ๋ผ๋ฏธํฐ์
๋๋ค. Matplotlib์
rcParams
์์
font.family
์
font.size
๊ฐ ํ๋ ์ผ๊ณผ ๋์ผํ ์ผ์ ์ํํฉ๋๋ค.
python
from
matplotlib
import
rcParams
rcParams
[
'font.family'
]
=
'Malgun Gothic'
rcParams
[
'font.size'
]
=
18
Matplotlib์
rcParams
์์์ฒ๋ผ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ ์ ๋ฐ์ ์กฐ์ ํ๊ณ ์ถ๋ค๋ฉด
rc
ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
rc
rc
ํ๋ผ๋ฏธํฐ๋ ์ถ(axes), ๊ทธ๋ฆฌ๋(grid), ๋๊ธ(ticks), ๊ธ๊ผด(font) ๋ฑ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ์ ์ ๋ฐ์ ์กฐ์ ํ๋ ํ๋ผ๋ฏธํฐ์
๋๋ค.
plotting_context()
ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ฌ ๊ทธ๋ํ์ ์ ์ฉ๋๊ณ ์๋ ์ค์ ๊ฐ์ ์ ์ ์์ต๋๋ค.
rc
ํ๋ผ๋ฏธํฐ์์ ์ฌ์ฉํ ์ ์๋ ์ค์ ๊ฐ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
sns
.
plotting_context
(
)
{
'axes.facecolor'
:
'white'
,
'axes.edgecolor'
:
'black'
,
'axes.grid'
:
False
,
'axes.axisbelow'
:
'line'
,
'axes.labelcolor'
:
'black'
,
'figure.facecolor'
:
'white'
,
'grid.color'
:
'#b0b0b0'
,
'grid.linestyle'
:
'-'
,
'text.color'
:
'black'
,
'xtick.color'
:
'black'
,
'ytick.color'
:
'black'
,
'xtick.direction'
:
'out'
,
'ytick.direction'
:
'out'
,
'lines.solid_capstyle'
:
<
CapStyle
.
projecting
:
'projecting'
>
,
'patch.edgecolor'
:
'black'
,
'patch.force_edgecolor'
:
False
,
'image.cmap'
:
'viridis'
,
'font.family'
:
[
'sans-serif'
]
,
'font.sans-serif'
:
[
'DejaVu Sans'
,
'Bitstream Vera Sans'
,
'Computer Modern Sans Serif'
,
'Lucida Grande'
,
'Verdana'
,
'Geneva'
,
'Lucid'
,
'Arial'
,
'Helvetica'
,
'Avant Garde'
,
'sans-serif'
]
,
'xtick.bottom'
:
True
,
'xtick.top'
:
False
,
'ytick.left'
:
True
,
'ytick.right'
:
False
,
'axes.spines.left'
:
True
,
'axes.spines.bottom'
:
True
,
'axes.spines.right'
:
True
,
'axes.spines.top'
:
True
}
์ฌ๊ธฐ์
axes.spines
์ ๊ทธ๋ํ์ ์ถ์ ๋ํ๋ด๊ฑฐ๋ ์จ๊ธฐ๋ ์ต์
์
๋๋ค. ๋ฐ๋ก ์ค์ ํ์ง ์์ผ๋ฉด Seaborn์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์(top), ์๋(bottom), ์ผํธ(left), ์ค๋ฅธํธ(right) ์ด 4๊ฐ์ ์ถ์ ๋ณด์ฌ์ค๋๋ค. ๋ง์ผ ์์ชฝ ์ถ๊ณผ ์ค๋ฅธ์ชฝ ์ถ์ ์จ๊ธฐ๊ณ ์ถ๋ค๋ฉด
despine()
ํจ์๋ฅผ ์ด์ฉํด๋ ๋ฉ๋๋ค.
despine()
ํจ์๋ ๋ฐ๋์ ๊ทธ๋ํ ํจ์ ๋ค์์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
python
sns
.
countplot
(
.
.
.
)
sns
.
despine
(
)
๋ง์ผ ํน์ ์ถ์ ์จ๊ธฐ๊ณ ์ถ๋ค๋ฉด ์๋์ ๊ฐ์ด ์จ๊ธฐ๊ณ ์ถ์ ๋ฐฉํฅ์ ํ๋ผ๋ฏธํฐ์
True
๊ฐ์ ์ ๋ฌํ๋ฉด ๋ฉ๋๋ค.
python
sns
.
countplot
(
.
.
.
)
sns
.
despine
(
left
=
True
,
bottom
=
True
)
set_style
set_style()
ํจ์๋ก๋ ๊ทธ๋ํ ์ ๋ฐ์ ์ ์ฉ๋ ํ
๋ง์ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ์ ์กฐ์ ํ ์ ์์ต๋๋ค.
python
rc
=
{
'grid.color'
:
'.5'
,
'grid.linestyle'
:
':'
}
sns
.
set_style
(
'whitegrid'
,
rc
=
None
)
set_context
set_context()
ํจ์๋ก๋ ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์ง์ ํ ์ ์์ต๋๋ค.
python
sns
.
set_context
(
'notebook'
,
font_scale
=
1.25
,
rc
=
{
'grid.color'
:
'.6'
}
)
set_palette
set_palette()
ํจ์๋ก๋ ๊ทธ๋ํ์ ์์ ํ๋ ํธ๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
python
sns
.
set_palatte
(
'colorblind'
)
๊ทธ๋ํ๋ณ ์คํ์ผ๋ง
๊ทธ๋ํ๋ณ ์คํ์ผ๋ง์ ํ๋ ค๋ฉด
set()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
์ถ ๋ฒ์ ์ ํํ๊ธฐ: xlim, ylim
Seaborn์์ x์ถ๊ณผ y์ถ์ ๋ฒ์๋ฅผ ์ค์ ํ๋ ค๋ฉด
xlim
,
ylim
ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
sns
.
countplot
(
.
.
.
)
.
set
(
xlim
=
(
1
,
10
)
,
ylim
=
(
0
,
20
)
)
์ถ ๋ผ๋ฒจ ์จ๊ธฐ๊ธฐ: xlabel, ylabel
Seaborn์์ ์ถ์ ์๋ ๋ผ๋ฒจ์ ์จ๊ธฐ๋ ค๋ฉด
xlabel
,
ylabel
ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
ax
=
sns
.
heatmap
(
.
.
.
)
ax
.
set
(
xlabel
=
""
,
ylabel
=
""
)
์ถ ์์น ๋ฐ๊พธ๊ธฐ
์ถ ์์น๋ฅผ ์กฐ์ ํ๋ ค๋ฉด
ax.axis.tick_top
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
ax
=
sns
.
heatmap
(
.
.
.
)
ax
.
xaxis
.
tick_top
(
)
ax
.
yaxis
.
tick_left
๊ทธ๋ํ ํฌ๊ธฐ ์กฐ์ ํ๊ธฐ
Seaborn์์ ๊ฐ๋ณ ๊ทธ๋ํ์ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ ค๋ฉด
rc
ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
sns
.
barplot
(
.
.
.
)
sns
.
set
(
rc
=
{
'figure.figsize'
:
(
10
,
7
)
}
)
์ค์น์ ๊ธฐ๋ณธ ํ๊ฒฝ ์ค์ ์ ๋ชจ๋ ๋ง์ณค๋ค๋ฉด ์ด์ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉ(loading)ํด์ค๊ฒ ์ต๋๋ค.
๋ฐ์ดํฐ
Seaborn์์๋ ๋ฐ์ดํฐ๋ฅผ ์ธ๋ถ์์ ๊ฐ์ ธ์ฌ ์๋ ์๊ณ , ๋ด์ฅ ๋ฐ์ดํฐ(built-in data)๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
1) ๋ฐ์ดํฐ ์ ํ
์ธ๋ถ ๋ฐ์ดํฐ
์ธ๋ถ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ฉํ๋ ค๋ฉด pandas๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. CSV ํ์ผ๊ณผ ์์
ํ์ผ์ DataFrame ๊ฐ์ฒด๋ก ๋ถ๋ฌ์ค๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
import
pandas
as
pd
df
=
pd
.
read_csv
(
'data.csv'
)
df
=
pd
.
read_excel
(
'data.xlsx'
)
pandas์์ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉํ๋ ์์ธํ ๋ฐฉ๋ฒ์
Python pandas ๋ฐ์ดํฐ ์์ฑ, ๋ก๋ฉ๊ณผ ์ ์ฅ, ์์ธ ๊ด๋ฆฌํ๋ ๋ฒ
์์ '๋ก๋ฉ ๋ฐ ์ ์ฅ' ํธ์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฌ๊ธฐ์๋ Seaborn์ ๋ด์ฅ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ฒ ์ต๋๋ค.
๋ด์ฅ ๋ฐ์ดํฐ
Seaborn์๋ ๋ค์ํ ๋ด์ฅ ๋ฐ์ดํฐ์
์ด ์์ต๋๋ค. ํจํค์ง ๋ด์ ์ด๋ค ๋ด์ฅ ๋ฐ์ดํฐ์
์ด ์๋์ง ์์๋ณด๋ ค๋ฉด
get_dataset_names()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
sns
.
get_dataset_names
(
)
[
'anagrams'
,
'anscombe'
,
'attention'
,
'brain_networks'
,
'car_crashes'
,
'diamonds'
,
'dots'
,
'exercise'
,
'flights'
,
'fmri'
,
'gammas'
,
'geyser'
,
'iris'
,
'mpg'
,
'penguins'
,
'planets'
,
'taxis'
,
'tips'
,
'titanic'
]
์ด ๋ฐ์ดํฐ์
์ค์์ ๋ช ๊ฐ์ง๋ฅผ ์ ํํด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
2) ๋ฐ์ดํฐ ๋ก๋ฉ
Seborn์ ๋ด์ฅ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉํ๋ ค๋ฉด
load_dataset()
ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. pandas๋ฅผ ์ด์ฉํด ๊ฐ์ ธ์จ ์ธ๋ถ ๋ฐ์ดํฐ์ ๋ง์ฐฌ๊ฐ์ง๋ก
load_dataset()
ํจ์๋ก ๋ถ๋ฌ์จ ๋ฐ์ดํฐ ํ์๋ DataFrame ๊ฐ์ฒด์
๋๋ค. ์ฌ๊ธฐ์๋ ๋ค์ ๋ฐ์ดํฐ์
์ ๋ถ๋ฌ์ ๋ณด๊ฒ ์ต๋๋ค.
python
df_titanic
=
sns
.
load_dataset
(
'titanic'
)
df_iris
=
sns
.
load_dataset
(
'iris'
)
df_penguins
=
sns
.
load_dataset
(
'penguins'
)
df_tips
=
sns
.
load_dataset
(
'tips'
)
df_diamonds
=
sns
.
load_dataset
(
'diamonds'
)
df_planets
=
sns
.
load_dataset
(
'planets'
)
3) ๋ฐ์ดํฐ ๊ตฌ์กฐ ํ์
๋ฐ์ดํฐ์
์ด ์ ์ค๋น๋์๋ค๋ฉด ๋ฐ์ดํฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ๋จํ ์ดํด๋ณด๊ฒ ์ต๋๋ค. pandas์์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ๋ ์์ธํ ๋ฐฉ๋ฒ์ ์๊ณ ์ถ์ผ์๋ค๋ฉด
Python pandas ๋ฐ์ดํฐ ํ์ธ, ์ ๋ ฌ, ์ ํํ๋ ๋ฒ
์์ "๋ฐ์ดํฐ ํ์ธ" ๋ถ๋ถ์ ์ฐธ์กฐํด ์ฃผ์ธ์.
python
df
.
shape
df
.
head
(
)
df
[
'class'
]
๋ฐ์ดํฐ์
์ด ์ ์ค๋น๋์๋ค๋ฉด ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์๊ฐํ๋ฅผ ํด๋ณด๊ฒ ์ต๋๋ค. ๊ธฐ๋ณธํธ์์ ์๊ฐํํ ๋ฐ์ดํฐ๋ 1์ฐจ์ ๋ฐ์ดํฐ์
๋๋ค. 1์ฐจ์ ๋ฐ์ดํฐ๋ ์์ฑ(attribute)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ์
๋๋ค. Numpy ๋ฐฐ์ด์์ ์์๋ฅผ ํ ์ค๋ก ๋ํ๋ผ ์ ์๋ ๋ฐ์ดํฐ, ์์
์์ ์ด(columns)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ, ๋
๋ฆฝ๋ณ์(independent variable) ๋๋ ๋ณ๋(variate)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ๋ผ๊ณ ๋ ๋ณผ ์ ์์ต๋๋ค.
1์ฐจ์ ๋ฐ์ดํฐ๋ ์์นํ๊ณผ ๋ฒ์ฃผํ์ผ๋ก ๋๋ฉ๋๋ค. ์์นํ์ ๋ณ์๊ฐ ์ค์ซ๊ฐ์ธ ์ฐ์์ ๋ณ์(continous variables)์ ์ ์ซ๊ฐ์ธ ์ด์ฐ์ ๋ณ์(discrete variables)์ธ ๋ฐ์ดํฐ์
๋๋ค. ๋ฒ์ฃผํ์ ๋ณ์๊ฐ ์นดํ
๊ณ ๋ฆฌ(category)์ฒ๋ผ ๋ถ๋ฅ๋ ์ง์ ๋ณ์(qualitative variables)๋ฅผ ์๋ฏธํฉ๋๋ค. ๊ทธ๋ผ 1์ฐจ์ ๋ฒ์ฃผํ ๋ฐ์ดํฐ๋ถํฐ ์๊ฐํํด ๋ณด๊ฒ ์ต๋๋ค.
1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ๋ฒ์ฃผํ
์ผ๋ณ๋ ๋ฒ์ฃผํ ์๋ฃ๋ฅผ ์๊ฐํํ๋ ๋ฐ๋ ๋ง๋๊ทธ๋ํ(bar graph)์ ํ์ด ์ฐจํธ(pie chart)๋ฅผ ์ด์ฉํฉ๋๋ค. ๋ค๋ง, Seaborn์๋ ํ์ด ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ธฐ๋ฅ์ด ์์ต๋๋ค. ํ์ด ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด Matplotlib์ ์ด์ฉํด์ผ ํฉ๋๋ค. ์์ธํ ๋ฐฉ๋ฒ์ ํ์ด์ฌ Matplotlib ์ฌ์ฉ๋ฒ(์์ )์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฌ๊ธฐ์๋ Seaborn์ผ๋ก ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
1) ๋น๋ ๋ง๋๊ทธ๋ํ: countplot()
Seaborn์ผ๋ก ๋ฒ์ฃผํ ๋ฐ์ดํฐ์ ๋ํ ๋น๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ํจ์๋
countplot()
์
๋๋ค. ์ด ํจ์๋ ๊ฐ ๋ฒ์ฃผ์ ์ํ๋ ๋ฐ์ดํฐ์ ๋น๋(๊ฐ์)๋ฅผ ๋ง๋์ ๋์ด๋ก ํ์ํฉ๋๋ค. ๋จผ์ ์์ง ๋น๋ ๋ง๋๊ทธ๋ํ๋ถํฐ ๊ทธ๋ ค๋ณด๊ฒ ์ต๋๋ค.
์์ง ๋น๋ ๋ง๋๊ทธ๋ํ
๊ธฐ๋ณธ
Seaborn์ผ๋ก ์์ง ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ธฐ๋ณธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
sns
.
countplot
(
df_titanic
[
'class'
]
)
sns
.
countplot
(
x
=
df_titanic
[
'class'
]
)
sns
.
countplot
(
x
=
'class'
,
data
=
df_titanic
)
์ฝ๋1์ ์๋์ ๊ฐ์ด ์ต์
์ ์ถ๊ฐํ ์๋ ์์ต๋๋ค.
color: ๋ง๋ ์ ์ง์
edgecolor: ๋ง๋ ํ
๋๋ฆฌ์ ์ง์
palette: ๊ทธ๋ํ ์ ์ง์
alpha: ๊ทธ๋ํ ํฌ๋ช
๋ ์ง์
linewidth: ๊ทธ๋ํ ๊ตต๊ธฐ ์ง์
palette
์ ๋ค์ํ ์ต์
์
Seaborn ๊ณต์ ํํ์ด์ง color palette
์์ ํ์ธํ์ค ์ ์์ต๋๋ค.
python
sns
.
countplot
(
x
=
'class'
,
data
=
df_titanic
,
color
=
'skyblue'
)
sns
.
countplot
(
x
=
'class'
,
data
=
df_titanic
,
palette
=
'Set3'
)
sns
.
countplot
(
x
=
'class'
,
data
=
df_titanic
,
facecolor
=
(
0
,
0
,
0
,
0
)
,
linewidth
=
5
,
edgecolor
=
sns
.
color_palette
(
'dark'
,
3
)
)
์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
countplot ํจ์๋ก ๊ทธ๋ฆฐ ์์ง ๋ง๋๊ทธ๋ํย
countplot()
ํจ์ ์ธ์๋
catplot()
ํจ์๋ฅผ ์ด์ฉํ ์๋ ์์ต๋๋ค.
catplot()
ํจ์๋ ์์นํ ๋ณ์์ ๋ฒ์ฃผํ ๋ณ์์ ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ ํจ์์ด์ง๋ง,
kind='count'
์ต์
์ ์ถ๊ฐํ๋ฉด ๋น๋๋ฅผ ๋ํ๋ด๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆด ์ ์์ต๋๋ค (์ฝ๋1๊ณผ ๋์ผ).
python
sns
.
catplot
(
x
=
'class'
,
kind
=
'count'
,
data
=
df_titanic
)
catplot() ํจ์๋ก ๊ทธ๋ฆฐ ์์ง ๋ง๋๊ทธ๋ํย
๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
๋ง์ผ ๋ง๋๊ทธ๋ํ๋ฅผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ๋ค๋ฉด
order
ํ๋ผ๋ฏธํฐ์
df.value_counts().index
์ฝ๋๋ฅผ ๋ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
df.value_counts().index
๋ ๋ณ์์ ๊ฐ์ ๋น๋๊ฐ ๋์ ์์ผ๋ก ์ ๋ ฌํด์ค๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
sns
.
countplot
(
x
=
'class'
,
data
=
df_titanic
,
order
=
df_titanic
[
'class'
]
.
value_counts
(
)
.
index
)
๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ ์์ง ๋ง๋๊ทธ๋ํย
์์ฝ๊ฐ ํ์
๊ฐ ๋ง๋ ์์ ์์ฝ๊ฐ์ ์ซ์๋ก ํ์ํ๊ณ ์ถ๋ค๋ฉด ๊ทธ๋ํ๋ฅผ ๋ณ์์ ํ ๋นํ ๋ค
ax.bar_label(ax.containers[0])
์ฝ๋๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค.
python
ax
=
sns
.
countplot
(
df_titanic
[
'class'
]
)
ax
.
bar_label
(
ax
.
containers
[
0
]
)
์์ฝ๊ฐ์ ํ์ํ ์์ง ๋ง๋๊ทธ๋ํย
์๊น ๊ฐ์กฐ
ํน์ ๋ง๋์ ์๊น์ ๊ฐ์กฐํ ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๊ฐ์ฅ ๋น๋์๊ฐ ๋์ ๋ง๋๋ง ๋ค๋ฅธ ์์ผ๋ก ํ์ํ๊ณ ์ถ๋ค๋ฉด
numpy
์
barplot()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
values
=
np
.
array
(
df_titanic
[
'class'
]
.
value_counts
(
)
)
idx
=
np
.
array
(
df_titanic
[
'class'
]
.
value_counts
(
)
.
index
)
palette
=
[
'skyblue'
if
(
x
==
max
(
values
)
)
else
'lightgrey'
for
x
in
values
]
sns
.
barplot
(
x
=
idx
,
y
=
values
,
palette
=
palette
)
ํน์ ๋ง๋๋ฅผ ๋ค๋ฅธ ์์ผ๋ก ๊ฐ์กฐํ ์์ง ๋ง๋๊ทธ๋ํย
์ํ ๋น๋ ๋ง๋๊ทธ๋ํ
Seaborn์ผ๋ก ์ผ๋ณ๋ ๋ฒ์ฃผํ ์๋ฃ์ ๋ํ ๊ฐ๋ก ๋น๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด
countplot()
ํจ์์ x ๋งค๊ฐ๋ณ์ ๋์ y ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
sns
.
countplot
(
y
=
'class'
,
data
=
df_titanic
)
sns
.
catplot
(
y
=
'class'
,
kind
=
'count'
,
palette
=
'ch:.25'
,
data
=
df_titanic
)
countplot() ํจ์๋ก ๊ทธ๋ฆฐ ์ํ ๋ง๋๊ทธ๋ํย
์ง๊ธ๊น์ง ์ผ๋ณ๋ ๋ฒ์ฃผํ ๋ฐ์ดํฐ๋ฅผ ๋ง๋๊ทธ๋ํ๋ก ์๊ฐํํ๋ ๋ฒ์ ์ดํด๋ณด์์ต๋๋ค. ์ด๋ฒ์๋ ์ผ๋ณ๋ ์์นํ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
2) ์์ ๊ทธ๋ฆผ
์์ ๊ทธ๋ฆผ(๋์์ด: box plot, ๋ฐ์ค ํ๋กฏ, ๋ฐ์ค ๊ทธ๋ํ, ์์ ๊ทธ๋ํ)์ ๋ฐ์ดํฐ์ 5๊ฐ์ง ํต๊ณ๋(์ต์๊ฐ, ์ 1 ์ฌ๋ถ์, ์ 2์ฌ๋ถ์, ์ 3์ฌ๋ถ์, ์ต๋๊ฐ)์ ๋ํ๋ด๋ ๊ทธ๋ํ์
๋๋ค. ์์๊ทธ๋ฆผ์ ๋ฐ์ดํฐ์ ์ ์ฒด์ ์ธ ๋ถํฌ์ ์ด์์น๋ฅผ ํ์ธํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
boxplot()
์์๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๋ ค๋ฉด
boxplot()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
sns
.
boxplot
(
data
=
df_iris
,
x
=
'sepal_length'
)
boxplot ํจ์๋ก ๊ทธ๋ฆฐ ์์ ์์ผ ๊ทธ๋ฆผย
catplot()
ํจ์์
kind='box'
์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
python
sns
.
catplot
(
data
=
df_iris
,
x
=
'sepal_length'
,
kind
=
'box'
)
boxenplot()
๋ฐ์ดํฐ์
์ ๋ฐ์ดํฐ ๋ฒ์๊ฐ ํด ๊ฒฝ์ฐ์๋
boxenplot()
์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
boxenplot()
์ ๋ฐ์ดํฐ๋ฅผ ๋ ๋ง์ ๋ถ์๋ก ๋๋์ด ํฌ๊ธฐ๊ฐ ํฐ ๋ฐ์ดํฐ์
์ ๋ฒ์ฃผ๋ฅผ ์์๊ทธ๋ฆผ์ผ๋ก ํํํด ์ค๋๋ค.
python
sns
.
boxenplot
(
data
=
df_diamonds
,
x
=
'price'
)
boxenplot() ํจ์๋ก ๊ทธ๋ฆฐ ์์๊ทธ๋ฆผย
catplot()
ํจ์์
kind='boxen'
์ต์
์ ์ฌ์ฉํด๋ ๋ฉ๋๋ค.
python
sns
.
catplot
(
data
=
df_diamonds
,
x
=
'price'
,
kind
=
'boxen'
)
violinplot()
์์ ๊ทธ๋ฆผ๊ณผ ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ํฉ์ณ์ ๊ทธ๋ฆฌ๋ ค๋ฉด
violinplot()
์ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
sns
.
violinplot
(
data
=
df_iris
,
x
=
'sepal_length'
)
violinplot() ํจ์๋ก ๊ทธ๋ฆฐ ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏย
python
sns
.
catplot
(
data
=
df_iris
,
x
=
'sepal_length'
,
kind
=
'violin'
)
์ ๊ทธ๋ํ์์ ๊ฐ์ด๋ฐ ํฐ์ ์ ์ ์ค์๊ฐ(median)์ ๋ํ๋
๋๋ค. ๋ํ, ํฐ์ ์ ์ ๋๋ฌ์ผ ๋๊บผ์ด ์ ์ ์ฌ๋ถ์ ๋ฒ์๋ฅผ, ๋๊บผ์ด ์ ์์ ์ ๋์ผ๋ก ์ด์ด์ง๋ ์์ ์ ์ 95% ์ ๋ขฐ ๊ตฌ๊ฐ์ ๋ํ๋
๋๋ค.
1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ์์นํ
์์นํ ๋ฐ์ดํฐ๋ ๋ถํฌ๋ฅผ ๋ณด๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
1) ์ ๊ทธ๋ํ: stripplot(), swarmplot()
์ ๊ทธ๋ํ(๋์์ด: dot graph, strip chart, ์ ๋ํ)๋ ๋ฐ์ดํฐ์ ์ค์ ์์น๋ฅผ ์ (dots)์ผ๋ก ๋ณด์ฌ์ค๋๋ค. ์ ๊ทธ๋ํ๋ ๋ฐ์ดํฐ์ ์ค์ ์์น๋ฅผ ํ์
ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค. Seaborn์ผ๋ก ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด
stripplot()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
sns
.
stripplot
(
data
=
df_iris
,
x
=
'sepal_length'
)
sns
.
stripplot
(
x
=
df_iris
[
'sepal_length'
]
)
stripplot() ํจ์๋ก ๊ทธ๋ฆฐ ์ ๊ทธ๋ํย
catplot()
ํจ์์
kind='strip'
์ต์
์ ์ถ๊ฐํด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
python
sns
.
catplot
(
data
=
df_iris
,
x
=
'sepal_length'
,
kind
=
'strip'
)
๋ค๋ง ์ ๊ทธ๋ํ์ ๊ฒฝ์ฐ ํํํ ๋ฐ์ดํฐ ํฌ์ธํธ๋ค์ด ๋ง์์ง๋ฉด ์ ๋ค์ด ๊ฒน์ณ ๋ณด์ผ ์ ์์ต๋๋ค. ์ด๋๋
swarmplot()
์ ์ด์ฉํด ์๋ฃ๋ฅผ ํฉํธ๋ ค์(jittering) ์ ์ฌ์ด์ ๊ฐ๊ฒฉ์ ์กฐ์ ํ๋ฉด ๋ฉ๋๋ค.
python
sns
.
swarmplot
(
data
=
df_iris
,
x
=
'sepal_length'
)
swarmplot() ํจ์๋ก ํํธ๋ ค ๊ทธ๋ฆฐ ์ ๊ทธ๋ํย
2) ์ ๋ถ๊ทธ๋ํ: rugplot()
์ ๋ถ๊ทธ๋ํ(rug plot) ๋๋ ๋ฌ๊ทธ ํ๋กฏ์ ๋ชจ๋ ๋ฐ์ดํฐ ํฌ์ธํธ๋ฅผ ์ถ ์์ ์์ ์ ๋ถ(rug)์ผ๋ก ๋ํ๋ธ ๊ทธ๋ํ์
๋๋ค. ์ ๋ถ ๊ทธ๋ํ์ ๊ฐ ์ ๋ถ์ ์ค์ ๋ฐ์ดํฐ๋ค์ ์์น๋ฅผ ๋ณด์ฌ์ค๋๋ค. ์ ๋ถ๋ค์ด ์ด์ดํ ์์์๋ก ๋ฐ์ดํฐ๊ฐ ๋ฐ์ง๋์ด ์์์ ์๋ฏธํฉ๋๋ค. ์ฃผ๋ณ ๋ถํฌ(marginal distribution)์ ๋ํ๋ด๋ฉฐ ์ฃผ๋ก ๋ค๋ฅธ ๊ทธ๋ํ์ ํจ๊ป ์ฌ์ฉ๋ฉ๋๋ค.
๋ฌ๊ทธ ํ๋กฏ์ ๊ทธ๋ฆฌ๋ ค๋ฉด
rugplot()
ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
displot()
ํจ์์
rug=True
์ต์
์ ์ฃผ์ด๋ ๋ฉ๋๋ค. ๋ฌ๊ทธ๋ฅผ ์ธ๋ถ์ ์ผ๋ก ์กฐ์ ํด์ผ ํ ๋๋
displot()
ํจ์๋ณด๋ค๋
rugplot()
ํจ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
height: ์ ๋ถ ๊ธธ์ด ์ง์
clip_on: ์ ๋ถ ์ถ ๋ฐ์ ๊ทธ๋ฆฌ๊ธฐ ์ง์
lw: ์ ๋ถ ์๊ธฐ ์ง์
alpha: ์ ๋ถ ํฌ๋ช
๋ ์ง์
python
sns
.
displot
(
data
=
df_tips
,
x
=
'total_bill'
,
rug
=
True
)
sns
.
rugplot
(
data
=
df_tips
,
x
=
'total_bill'
,
height
=
.1
)
sns
.
rugplot
(
data
=
df_tips
,
x
=
'total_bill'
,
height
=
-
.02
,
clip_on
=
False
)
sns
.
rugplot
(
data
=
df_diamonds
,
x
=
'carat'
,
lw
=
1
,
alpha
=
.005
)
rugplot() ํจ์๋ก ๊ทธ๋ฆฐ ๋ค์ํ ์ ๋ถ๊ทธ๋ํย
๋ฐ์ดํฐ๊ฐ ๋ง์์ง ๊ฒฝ์ฐ ์ ๊ทธ๋ํ ๋๋ ์ ๋ถ๊ทธ๋ํ๋ง์ผ๋ก ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ํ๋์ ํ์
ํ๊ธฐ ์ด๋ ต์ต๋๋ค. ๋ฐ์ดํฐ ํฌ์ธํธ๊ฐ ์๋ก ๊ฒน์ณ ๋ณด์ด๊ธฐ ๋๋ฌธ์
๋๋ค. ์ด๋ ์ฌ์ฉํ๋ฉด ์ข์ ๋๊ตฌ๊ฐ ํ์คํ ๊ทธ๋จ์
๋๋ค.
3) ํ์คํ ๊ทธ๋จ: histplot()
ํ์คํ ๊ทธ๋จ(histogram)์ ์์นํ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ๊ตฌ๊ฐ๋ณ ๋น๋์๋ก ํํํ ๊ทธ๋ํ์
๋๋ค. ์ฆ, ๋ฐ์ดํฐ๋ฅผ ๋ช ๊ฐ์ ๊ตฌ๊ฐ์ผ๋ก ๋๋ ํ ๊ฐ ๊ตฌ๊ฐ์ ํฌํจ๋ ๋ฐ์ดํฐ์ ๊ฐ์ ๋๋ ๋์(frequency)๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ๋ํ๋ธ ๊ฒ์
๋๋ค. ๋ฐ์ดํฐ๊ฐ ๊ตฌ๊ฐ์ผ๋ก ์ฒ๋ฆฌ๋๊ธฐ ๋๋ฌธ์, ๋ฒ์๊ฐ ๋์ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ํ์
ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
๊ธฐ๋ณธ
Seaborn์์ ๋ณ์๊ฐ 1๊ฐ์ธ ๋จ๋ณ๋ ํ์คํ ๊ทธ๋จ์ ๋ง๋ค๋ ค๋ฉด
histplot()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์ปค๋๋ฐ๋์ถ์ (Kernel Density Estimation, KDE) ๋ฐฉ๋ฒ์ผ๋ก ์ค๋ฌด๋ฉ(smoothing)ํ ํ์คํ ๊ทธ๋จ์ ํ๋ฅ ๋ฐ๋ํจ์(Probability Density Function, PDF)๋ฅผ ํจ๊ป ๊ทธ๋ฆฌ๊ณ ์ถ๋ค๋ฉด
kde=True
์ต์
์ ์ฃผ๋ฉด ๋ฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
sns
.
histplot
(
df_penguins
,
x
=
'flipper_length_mm'
)
sns
.
histplot
(
df_penguins
[
'flipper_length_mm'
]
,
kde
=
True
)
displot()
ํจ์๋ฅผ ์ด์ฉํด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
displot()
ํจ์์ ์ด๊ธฐ ๊ธฐ๋ณธ ์ค์ ์
kind='hist'
์
๋๋ค.
displot()
ํจ์์
kind
์ต์
์ ๋ฐ๋ก ์ง์ ํ์ง ์์ผ๋ฉด ํ์คํ ๊ทธ๋จ์ ๊ทธ๋ฆฝ๋๋ค.
python
sns
.
displot
(
df_penguins
,
x
=
'flipper_length_mm'
)
sns
.
displot
(
df_penguins
[
'flipper_length_mm'
]
,
kde
=
True
)
histplot() ํจ์๋ก ๋ง๋ ๋จ๋ณ๋ ํ์คํ ๊ทธ๋จย
python
sns
.
displot
(
df_diamonds
,
x
=
'carat'
,
kde
=
True
)
ํ์คํ ๊ทธ๋จ๊ณผ ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์ย
ํน์ ์กฐ๊ฑด
pandas๋ฅผ ์ด์ฉํ๋ฉด ๋ฐ์ดํฐ๋ฅผ ์กฐ๊ฑด๋ณ๋ก ์ ํํ ์ ์์ต๋๋ค. ๋ค์ ์ฝ๋๋ 'species'๊ฐ 'Adelie'์ธ ํญ๊ท์ 'flipper_length_mm'๋ฅผ ๊ด์ธกํ ๊ฐ์ ๋ํด ํ์คํ ๊ทธ๋จ์ ๊ทธ๋ฆฌ๋ ์์ ์
๋๋ค.
python
sns
.
histplot
(
df_penguins
[
df_penguins
[
'species'
]
==
'Adelie'
]
[
'flipper_length_mm'
]
)
๋ฑ๊ธ ์์ ๋ฑ๊ธ ํญ: bins, binwidth
ํ์คํ ๊ทธ๋จ์์๋ ๋ฑ๊ธ์ ์ ๋๋ ๋ฑ๊ธ์ ํญ์ ๋ฐ๋ผ ๊ทธ๋ํ์ ๋ชจ์์ด ๋ฌ๋ผ์ง๋๋ค. ๋ฑ๊ธ์ ์๋
bins
์ต์
์ผ๋ก, ๋ฑ๊ธํญ์
binwidth
์ต์
์ผ๋ก ์ง์ ํฉ๋๋ค.
python
sns
.
histplot
(
df_penguins
,
x
=
'flipper_length_mm'
,
bins
=
10
)
sns
.
histplot
(
df_penguins
,
x
=
'flipper_length_mm'
,
binwidth
=
3
)
๊ตฌ๊ฐ์ ์์ ๊ตฌ๊ฐ์ ํญ์ ์กฐ์ ํ ํ์คํ ๊ทธ๋จย
๋ฑ๊ธ๋ช
, ๋ฑ๊ธ๋ช
์์น, ๋ฑ๊ธ ์ฌ์ด ๊ณต๊ฐ: bins=๋ฆฌ์คํธ, discrete=True, shrink
ํ์คํ ๊ทธ๋จ์ ์ฃผ๋ก ์ฐ์์ ์๋ฃ๋ฅผ ์๊ฐํํ ๋ ์ฐ์ด์ง๋ง, ์ข
์ข
์ด์ฐ์ ์๋ฃ(discrete data)๋ฅผ ์๊ฐํํ๋ ๋ฐ๋ ์ฌ์ฉํฉ๋๋ค. ์ด์ฐ์ ์๋ฃ๋ ๋ง๋๊ทธ๋ํ๋ก ํํํฉ๋๋ค. ๋จ, ํ์คํ ๊ทธ๋จ์ ๋ง๋๊ทธ๋ํ๋ก ๋ณํํ ๋๋ ๋ค์๊ณผ ๊ฐ์ ์กฐ์ ์ด ํ์ํฉ๋๋ค.
๋ฑ๊ธ ๋ช
์
๋ฑ๊ธ๋ช
์ด ๋ง๋์ ์ค์์ ์ค๋๋ก ์์น
๋ณ์๊ฐ ์ฐ์์ ์ด์ง ์๊ณ ์ด์ฐ์ ์ด๋ผ๋ ๊ฒ์ ์๋ ค์ฃผ๊ธฐ ์ํด ๋ฑ๊ธ๊ณผ ๋ฑ๊ธ ์ฌ์ด์ ์ฌ์ ๋๊ธฐ
์ด๋ฅผ ๋์์ฃผ๋ ์ต์
์ด ๊ฐ๊ฐ
bins=๋ฆฌ์คํธ
,
discrete=True
,
shrink
์
๋๋ค. ๊ฐ ์ต์
์ด ์ํํ๋ ์ผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
bins=๋ฆฌ์คํธ: ๊ทธ๋ํ์ x์ถ์ ๋ช
์ํ ๋ฑ๊ธ์ ์ง์ ์ง์
discrete=True: ๊ฐ ๋ฑ๊ธ์ด ๋ง๋ ์ค์์ ์ค๋๋ก ์์น
shrink: ๊ฐ ๋ง๋ ์ฌ์ด์ ๊ณต๊ฐ์ ๋ง๋ จ
์์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
sns
.
histplot
(
df_tips
,
x
=
'size'
)
sns
.
histplot
(
df_tips
,
x
=
'size'
,
bins
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
]
)
sns
.
histplot
(
df_tips
,
x
=
'size'
,
discrete
=
True
)
sns
.
histplot
(
df_tips
,
x
=
'size'
,
discrete
=
True
,
shrink
=
.8
)
๋ฑ๊ธ๋ช
๊ณผ ๋ฑ๊ธ๋ช
์ ์์น, ๋ฑ๊ธ ์ฌ์ด์ ๊ณต๊ฐ์ ์กฐ์ ํ ํ์คํ ๊ทธ๋จย
๋จ, ํ์คํ ๊ทธ๋จ์ ๋ฐ์ดํฐ์ ์ฐ์์ ํน์ฑ์ ์จ์ ํ ํํํ์ง ๋ชปํ๋ ๋จ์ ์ด ์์ต๋๋ค. ๊ตฌ๊ฐ์ ํฌ๊ธฐ์ ์์์ ์ ๋ฐ๋ผ ๋ถํฌ์ ๋ชจ์์ด ํฌ๊ฒ ๋ฌ๋ผ์ง ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ฐ์ํ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ๋ถ๋๋ฌ์ด ๊ณก์ ์ผ๋ก ํํํ๋ ๋ฐฉ๋ฒ์ด ์์๊น์?
4) ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์: kdeplot()
์ปค๋๋ฐ๋์ถ์ (Kernel density estimation)์ ์ฃผ์ด์ง ๋ฐ์ดํฐ ํฌ์ธํธ๋ค์ ๋ฐํ์ผ๋ก ์ ์ฒด ๋ฐ์ดํฐ์ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ๋ถ๋๋ฌ์ด ๊ณก์ ์ผ๋ก ์ถ์ ํ๋ ๊ธฐ๋ฒ์
๋๋ค. ์์นํ ๋ฐ์ดํฐ๋ฅผ ์ด์ฐ์ ์ผ๋ก ํํํ๋ ํ์คํ ๊ทธ๋จ ๋์ ๋งค๋๋ฌ์ด ๊ณก์ ์ผ๋ก ํํํ๊ณ ์ถ๋ค๋ฉด ์ปค๋ ๋ฐ๋ ํจ์(Kernel density function)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๊ธฐ๋ณธ
kdeplot()
ํจ์๋ ๋จ๋ณ๋ ๋๋ ์ด๋ณ๋ ๋ฐ์ดํฐ์ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ์ถ์ ํ์ฌ ๊ทธ๋ฆฝ๋๋ค. ์ฌ๊ธฐ์ ํํ๋(bandwidth)์ ์กฐ์ ํ๋ ค๋ฉด
bw_adjust
์ต์
์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. ์ด ์ต์
์ ํฌ๊ธฐ์ ๋ฐ๋ผ ํ๋ฅ ๋ฐ๋ํจ์์ ๋ถ๋๋ฌ์(smoothness) ์ ๋๊ฐ ๋ฌ๋ผ์ง๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
python
sns
.
kdeplot
(
data
=
df_penguins
,
x
=
'flipper_length_mm'
)
sns
.
kdeplot
(
data
=
df_penguins
,
x
=
'flipper_length_mm'
,
bw_adjust
=
.25
)
์ฝ๋
2
displot()
ํจ์์
kind='kde'
์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
python
sns
.
displot
(
penguins
,
x
=
'flipper_length_mm'
,
kind
=
'kde'
)
sns
.
displot
(
penguins
,
x
=
'flipper_length_mm'
,
kind
=
'kde'
,
bw_adjust
=
.25
)
kdeplot ํจ์๋ก ๊ทธ๋ฆฐ ํ๋ฅ ๋ฐ๋ํจ์ย
๋ง์ผ
displot()
ํจ์์
kde=True
์ต์
์ ์ง์ ํ๋ฉด ํ์คํ ๊ทธ๋จ๊ณผ ์ปค๋๋ฐ๋์ถ์ ํจ์๋ฅผ ๋์์ ๊ทธ๋ฆด ์ ์์ต๋๋ค.
๋ฒ์ ์ ํ: cut
์ฐ์์ ๋ณ์๊ฐ ๋ฌดํํ ์ปค์ง์ง ์๋ ๊ฒฝ์ฐ์๋
cut
์ด๋ผ๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํด ์ ๋์ ๋ฐ์ดํฐ ํฌ์ธํธ ๋ฒ์๋ฅผ ์ ํํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
python
sns
.
kdeplot
(
df_tips
,
x
=
'total_bill'
,
kind
=
'kde'
)
sns
.
kdeplot
(
df_tips
,
x
=
'total_bill'
,
kind
=
'kde'
,
cut
=
0
)
์ ๋์ ๋ฐ์ดํฐ ํฌ์ธํธ ๋ฒ์๋ฅผ ์ ํํ ํ๋ฅ ๋ฐ๋ํจ์ย
์์: shade
KDE ๋ฐ๋ ๊ณก์ ์๋์ ์์ ์ฑ์ฐ๊ณ ์ถ๋ค๋ฉด
shade=True
์ต์
์ ์ฃผ๋ฉด ๋ฉ๋๋ค.
5) ๊ฒฝํ์ ๋์ ๋ถํฌํจ์: edcfplot()
๊ฒฝํ์ ๋์ ๋ถํฌํจ์(empirical cumulative distribution function, ECDF)๋ n๊ฐ์ ๋ฐ์ดํฐ ํฌ์ธํธ ๊ฐ๊ฐ์์ 1/n ์ฉ ์ ํํ๋ ๊ณ๋จ ํจ์์
๋๋ค. ๊ฐ๋จํ CDF๋ผ๊ณ ๋ ํฉ๋๋ค. ECDF ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด
ecdfplot()
ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
python
sns
.
ecdfplot
(
df_penguins
,
x
=
'flipper_length_mm'
)
ecdfplot() ํจ์๋ก ๊ทธ๋ฆฐ ๊ฒฝํ์ ๋์ ๋ถํฌํจ์ย
displot()
ํจ์์
kind='ecdf'
์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
python
sns
.
displot
(
df_penguins
,
x
=
'flipper_length_mm'
,
kind
=
'ecdf'
)
์ง๊ธ๊น์ง ํ์ด์ฌ Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก 1์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด์์ต๋๋ค. ๋ค์ ์๊ฐ์๋
ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ์ฌํํธ
์์ ๋ค์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ๋ชจ๋ ์๊ณ ๋ง์ผ์
จ์ต๋๋ค.
์ฐธ๊ณ ๋ฌธํ
[1] ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค ์ค์ฟจ, ๏ฝขํ์ด์ฌํธ 5์ฅ ๋ฐ์ดํฐ ์๊ฐํ: Seaborn์ ์ฌ์ฉํ ๋ฐ์ดํฐ ๋ถํฌ ์๊ฐํ๏ฝฃ, ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค ์ค์ฟจ, "
https://datascienceschool.net/
"
[2] ์๋ฌดํผ์๋ผ๋ฒจ, ๏ฝข์ ๊ทํ(Normalization) ์ฝ๊ฒ ์ดํดํ๊ธฐ๏ฝฃ, ์๋ฌดํผ์๋ผ๋ฒจ, "
https://hleecaster.com/ml-normalization-concept/
"
[3] ์๋ฌดํผ์๋ผ๋ฒจ, ๏ฝข[seaborn] ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ๏ฝฃ, ์๋ฌดํผ์๋ผ๋ฒจ, "
https://hleecaster.com/python-seaborn-violinplot/
"
[4] Codeacademy Team, ๏ฝขSeaborn Styling, Part 1: Figure Style and Scale๏ฝฃ, Codecademy, "
https://www.codecademy.com/article/seaborn-design-i
"
[5] Codeacademy Team, ๏ฝขSeaborn Styling, Part 2: Color๏ฝฃ, Codecademy, "
https://www.codecademy.com/article/seaborn-design-ii
"
[6] Mahbubul Alam, ๏ฝขSeaborn can do the job, then why Matplotlib?๏ฝฃ, Towards Data Science, "
https://towardsdatascience.com/seaborn-can-do-the-job-then-why-matplotlib-dac8d2d24a5f
"
[7] Seaborn, ๏ฝขseaborn: statistical data visualization๏ฝฃ, Seaborn, "
https://seaborn.pydata.org/index.html
"
[8] StackOverflow, ๏ฝขHow to set a different color to the largest bar in a seaborn barplot๏ฝฃ, StackOverflow, "
https://stackoverflow.com/questions/31074758/how-to-set-a-different-color-to-the-largest-bar-in-a-seaborn-barplot
"
๋ค์ ๊ธ |
| Markdown | [](https://www.snugarchive.com/)
- [ํ](https://www.snugarchive.com/)
- [๊ธ](https://www.snugarchive.com/blog/posts/)
- [ํ๋ก์ ํธ](https://www.snugarchive.com/blog/projects/)
- [๋ฌธ์](https://www.snugarchive.com/contact/)
- [ํ๊ทธ](https://www.snugarchive.com/tags/)
- [ํ](https://www.snugarchive.com/)
- [๊ธ](https://www.snugarchive.com/blog/posts/)
- [์ปดํจํฐ ๊ณผํ](https://www.snugarchive.com/blog/category/computer-science/)
- [๋ฐ์ดํฐ ๊ณผํ](https://www.snugarchive.com/blog/category/data-science/)
- [ํ๊ฒฝ ์ค์ ](https://www.snugarchive.com/blog/category/environment-setup/)
- [์ํ](https://www.snugarchive.com/blog/category/mathematics/)
- [์์ฐ์ด](https://www.snugarchive.com/blog/category/natural-language/)
- [ํ๋ก๊ทธ๋๋ฐ ์ธ์ด](https://www.snugarchive.com/blog/category/programming-language/)
- [์น ๊ฐ๋ฐ](https://www.snugarchive.com/blog/category/web-development/)
- [ํ๋ก์ ํธ](https://www.snugarchive.com/blog/projects/)
- [๋ฌธ์](https://www.snugarchive.com/contact/)
- [ํ๊ทธ](https://www.snugarchive.com/tags/)
[๋ฐ์ดํฐ ๊ณผํ](https://www.snugarchive.com/blog/category/data-science/)[๋ฐ์ดํฐ ์๊ฐํ](https://www.snugarchive.com/blog/category/data-science/visualization/)
# ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ๊ธฐ์ดํธ
## 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ
Last Updated
2024-09-10
Published
2022-06-05
- [Python Seaborn](https://www.snugarchive.com/tag/python-seaborn/)
8๋ถ
#### ๋ชฉ์ฐจ
1. [์ค๋นํ๊ธฐ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%A4%80%EB%B9%84%ED%95%98%EA%B8%B0)
- [์๋ด ์ฌํญ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%95%88%EB%82%B4-%EC%82%AC%ED%95%AD)
- [์ค์นํ๊ธฐ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0)
- [๊ธฐ๋ณธ ํ๊ฒฝ ์ค์ ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EA%B8%B0%EB%B3%B8-%ED%99%98%EA%B2%BD-%EC%84%A4%EC%A0%95)
- [์ ์ฒด ์คํ์ผ๋ง](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%A0%84%EC%B2%B4-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
- [๊ทธ๋ํ๋ณ ์คํ์ผ๋ง](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EA%B7%B8%EB%9E%98%ED%94%84%EB%B3%84-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
- [๋ฐ์ดํฐ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EB%8D%B0%EC%9D%B4%ED%84%B0)
2. [1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ๋ฒ์ฃผํ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#1%EC%B0%A8%EC%9B%90-%EB%8D%B0%EC%9D%B4%ED%84%B0-%EC%8B%9C%EA%B0%81%ED%99%94-%EB%B2%94%EC%A3%BC%ED%98%95)
- [1\) ๋น๋ ๋ง๋๊ทธ๋ํ: countplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#1-%EB%B9%88%EB%8F%84-%EB%A7%89%EB%8C%80%EA%B7%B8%EB%9E%98%ED%94%84-countplot)
- [2\) ์์ ๊ทธ๋ฆผ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#2-%EC%83%81%EC%9E%90-%EA%B7%B8%EB%A6%BC)
3. [1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ์์นํ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#1%EC%B0%A8%EC%9B%90-%EB%8D%B0%EC%9D%B4%ED%84%B0-%EC%8B%9C%EA%B0%81%ED%99%94-%EC%88%98%EC%B9%98%ED%98%95)
- [1\) ์ ๊ทธ๋ํ: stripplot(), swarmplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#1-%EC%A0%90%EA%B7%B8%EB%9E%98%ED%94%84-stripplot-swarmplot)
- [2\) ์ ๋ถ๊ทธ๋ํ: rugplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#2-%EC%84%A0%EB%B6%84%EA%B7%B8%EB%9E%98%ED%94%84-rugplot)
- [3\) ํ์คํ ๊ทธ๋จ: histplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#3-%ED%9E%88%EC%8A%A4%ED%86%A0%EA%B7%B8%EB%9E%A8-histplot)
- [4\) ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์: kdeplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#4-%EC%BB%A4%EB%84%90%EB%B0%80%EB%8F%84%EC%B6%94%EC%A0%95%EC%9C%BC%EB%A1%9C-%EA%B5%AC%ED%95%9C-%ED%99%95%EB%A5%A0%EB%B0%80%EB%8F%84%ED%95%A8%EC%88%98-kdeplot)
- [5\) ๊ฒฝํ์ ๋์ ๋ถํฌํจ์: edcfplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#5-%EA%B2%BD%ED%97%98%EC%A0%81-%EB%88%84%EC%A0%81%EB%B6%84%ED%8F%AC%ED%95%A8%EC%88%98-edcfplot)
4. [์ฐธ๊ณ ๋ฌธํ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%B0%B8%EA%B3%A0-%EB%AC%B8%ED%97%8C)




Seaborn์ผ๋ก ์ผ๋ณ๋ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํ ํด๋ณด์
[Snug Archive](https://www.snugarchive.com/)
ํ์ด์ฌ(Python)์๋ [Matplotlib(๋งทํ๋กฏ๋ฆฝ)](https://matplotlib.org/), Plotly(ํ๋กํ๋ฆฌ), GGplot(์ง์งํ๋กฏ) ๋ฑ ๋ค์ํ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์์ต๋๋ค. Matplotlib์ ์ ์ธ๊ณ์ ๋ฐ์ดํฐ ๊ณผํ์๋ค์ด ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. Plotly๋ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ plotly.js๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค์ด์ ธ, ๊ทธ๋ํ์ ํน์ ๋ถ๋ถ์ ํ๋/์ถ์ํ๊ฑฐ๋ ์ ์ฅํ๋ ๋ฑ ์น ์์์ ์ฌ์ฉ์์ ์ํธ์์ฉํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. GGplot์ R์ ggplot2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ฐํ์ผ๋ก ๊ฐ๋ฐ๋์ด, ๊ธฐ์กด์ R ์ฌ์ฉ์๋ค์ด ์ฌ์ฉํ๊ธฐ ํธ๋ฆฌํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
๊ทธ๋ ๋ค๋ฉด Seaborn์ด๋ ๋ฌด์์ผ๊น์? ๋, ๋ง์ ์๊ฐํ ๋๊ตฌ ์ค์์ Seaborn์ ์ฌ์ฉํ๋ฉด ์ข์ ์ด์ ๋ ๋ฌด์์ผ๊น์? Seaborn์ Matplotlib์ ์ฝ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด ๊ฐ๋ฐ๋ ๊ณ ์์ค(high-level) ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. Seaborn์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ ๊ฐ๊ฒฐํจ์
๋๋ค. Seaborn์ ์ด์ฉํ๋ฉด ๋น๊ต์ ์งง์ ์ฝ๋๋ก๋ ํต๊ณํ์ ์ฃผ์ ๊ทธ๋ํ๋ฅผ ๋น ๋ฅด๊ณ ํธ๋ฆฌํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. ๊ทธ๋ํ๋ฅผ ์ธ๋ถ ์ค์ ์์ด ๊ฐ๋จํ๊ณ ๋น ๋ฅด๊ฒ ๊ทธ๋ฆฌ๊ณ ์ถ๋ค๋ฉด Matplotlib๋ณด๋ค Seaborn์ ์ฌ์ฉํ๋ฉด ์ข์ต๋๋ค.
Seaborn์ ์ฌ์ฉ๋ฒ์ ๊ธฐ์ดํธ๊ณผ ์ฌํํธ์ผ๋ก ๋๋ฉ๋๋ค. ๊ธฐ์ดํธ์์๋ Seaborn์ ์ค์นํ๊ณ ์ค์ต์ ์ํ ๊ธฐ๋ณธ์ ์ธ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ๊ณผ ๋ณ์๊ฐ 1๊ฐ์ธ 1์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ๋ค๋ฃน๋๋ค. [ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ์ฌํํธ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-advanced/)์์๋ ๋ณ๋์ด 2๊ฐ ์ด์์ธ ๋ค์ฐจ์ ๊ทธ๋ํ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ๊ธฐ๋ณธํธ์์ ๋ค๋ฃฐ ์ ์ฒด ๊ทธ๋ํ์ ๊ฐ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.




Seaborn 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ ๋ก๋๋งต
๊ทธ๋ผ Seaborn์ผ๋ก 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ๋ฅผ ํ๊ธฐ ์ ์ ์ค๋นํ ์ฌํญ๋ถํฐ ์์๋ณด๊ฒ ์ต๋๋ค.
## ์ค๋นํ๊ธฐ
### ์๋ด ์ฌํญ
Seaborn์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๊ธฐ ์ํด์๋ ๋ช ๊ฐ์ง ์ค๋น๊ฐ ํ์ํฉ๋๋ค. ์ฒซ์งธ, ์ค์ต ํ๊ฒฝ์
๋๋ค. ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค(Data Science)๋ฅผ ์ํ ํตํฉ๊ฐ๋ฐํ๊ฒฝ(IDE)์๋ ๋ํ์ ์ผ๋ก ์คํ์ด๋(Spyder), ์ํฐ(Atom), ํ์ด์ฐธ(PyCharm) ๋ฑ์ด ์์ต๋๋ค. ์ด ๊ธ์์๋ ์ฃผํผํฐ ๋
ธํธ๋ถ(Jupyter Notebook)์ ์ฌ์ฉํฉ๋๋ค. ์ฃผํผํฐ ๋
ธํธ๋ถ์ ์์ธํ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ์ [์ฃผํผํฐ ๋
ธํธ๋ถ ํ๊ฒฝ ์ค์ ํ๊ธฐ](https://www.snugarchive.com/blog/jupyter-notebook-setup/)๋ฅผ ์ฐธ์กฐํด ์ฃผ์ธ์.
๋์งธ, ํต๊ณ ์ฉ์ด์ ๋ํ ๊ฐ๋จํ ์ดํด๊ฐ ํ์ํฉ๋๋ค. ๊ฐ ์ฉ์ด๋ ๊ฐ๋ตํ ์ค๋ช
ํ ์์ ์ด๋, ๊ฐ๋
์ ๋ํ ์์ธํ ์ค๋ช
์ด๋ ์์์ ๋ค๋ฃจ์ง ์์ต๋๋ค. ํต๊ณ ์ฉ์ด๋ฅผ ์ฐธ์กฐํ๋ฉด์ ๊ธ์ ์ฝ๊ณ ์ถ์ ๋ถ๋ค์ [ํ๋ฅ ๊ณผ ํต๊ณ ๊ธฐ์ด ์ฉ์ด](https://www.snugarchive.com/blog/glossary-statistical-terms/)๋ฅผ ํจ๊ป ์ฝ์ผ์๊ธฐ๋ฅผ ๊ถํด๋๋ฆฝ๋๋ค.
์
์งธ, Seaborn ํจ์์ ์ข
๋ฅ์
๋๋ค. Seaborn์ ์๊ฐํ ํจ์๋ ๊ทธ๋ํ ์์ค(figure-level)์ ํจ์์ ์ถ ์์ค(axes-level)์ ํจ์๋ก ๋๋ฉ๋๋ค. ๊ทธ๋ํ ์์ค์ ํจ์๋ ์์ ํจ์๋ก ๊ทธ๋ํ์ ์ข
๋ฅ๋ฅผ ์ง์ ํ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํด ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํฉ๋๋ค. ๋ฐ๋ฉด ์ถ ์์ค ํจ์๋ ๊ฐ ๊ทธ๋ํ์ ์ข
๋ฅ์ ํนํ๋ ํจ์์
๋๋ค. ์ถ ์์ค ํจ์๋ 1๊ฐ์ง ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฐ ๋ง์ถคํ๋์ด ์์ต๋๋ค.
์ด ๋ ์ข
๋ฅ์ ํจ์๋ฅผ ๊ตฌ๋ถํ๋ ๊ธฐ์ค์ `Grid`์ ์์ฑ ์ฌ๋ถ์
๋๋ค. `displot()`, `catplot()`, `relplot()` ํจ์๋ ๋ชจ๋ ๊ทธ๋ํ ์์ค ํจ์์ด๋ฉฐ `seaborn.axisgrid.FacetGrid`๋ฅผ ๋ง๋ญ๋๋ค. ๋ฐ๋ฉด, `countplot()`, `hisplot()`, `striplot()` ๋ฑ๊ณผ ๊ฐ์ ํจ์๋ ์ถ ์์ค ํจ์์ด๋ฉฐ ๊ฒฐ๊ณผ๋ก `AxesSubplot`์ ๋ง๋ญ๋๋ค. `FacetGrid`๋ ์ฌ๋ฌ ๊ทธ๋ํ๋ฅผ ํฌํจํ๋ ์์ ๊ทธ๋ํ๋ก, `FacetGrid`์์ ํน์ ํ์ `AxesSubplot` ๊ทธ๋ํ๋ง ์ถ์ถํด ์ํ๋ ์กฐ๊ฑด์ ์ ์ฉํ ์ ์์ต๋๋ค.
์ฌ์ฉํ๋ ์ต์
์ด ์๋ก ๋ค๋ฅธ ๊ฒฝ์ฐ๋ ์์ง๋ง ๋ณดํต ๊ทธ๋ํ ์์ค ํจ์์ ์ถ ์์ค ํจ์์ ์ต์
์ ์๋ก ํธํ๋ฉ๋๋ค. ๋ค๋ง, Matplotlib ๊ณผ์ ํธํ์ฑ์ด๋ ํ ๊ทธ๋ํ ์์ ๋ค๋ฅธ ๊ทธ๋ํ๋ฅผ ๊ฒน์ณ ๊ทธ๋ฆด ์ ์๋ค๋ ์ ์์๋ ์ถ ์์ค์ ํจ์๊ฐ ๊ทธ๋ํ ์์ค ํจ์๋ณด๋ค ์กฐ๊ธ ๋ ์ ์ฐํฉ๋๋ค. ์ฌ๊ธฐ์๋ ๊ทธ๋ํ ์์ค์ ํจ์๋ฅผ ์ค์ฌ์ผ๋ก ๊ทธ๋ํ๋ฅผ ์ดํด๋ณด๋, ๊ทธ๋ํ ์์ค์ผ๋ก ๊ทธ๋ฆด ์ ์๋ ๊ทธ๋ํ๋ ์ถ ์์ค ํจ์๋ก ๊ทธ๋ฆฌ๊ฒ ์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ์ง๊ธ๋ถํฐ Seaborn์ ์ค์นํด ๋ณด๊ฒ ์ต๋๋ค.
### ์ค์นํ๊ธฐ
#### 1\) ํ์ด์ฌ ๋ฐ pip ์ค์น ์ฌ๋ถ ํ์ธ
Seaborn์ ์ฌ์ฉํ๋ ค๋ฉด ํ์ด์ฌ๊ณผ ํ์ด์ฌ์ ํจํค์ง ๊ด๋ฆฌ ๋งค๋์ ์ธ `pip`์ด ํ์ํฉ๋๋ค. ์ฌ์ฉํ๊ณ ์๋ ์์คํ
์ ํ์ด์ฌ๊ณผ `pip`์ด ์ค์น๋์ด ์๋์ง ํ์ธํฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonpython -V # ํ์ด์ฌ ์ค์น ์ฌ๋ถ ํ์ธpip -v # pip ์ค์น ์ฌ๋ถ ํ์ธ
```
ํ์ด์ฌ๊ณผ `pip`์ด ์ ์ค์น๋์ด ์๋ค๋ฉด Seaborn์ ์ค์นํ ์ค๋น๊ฐ ๋ ๊ฒ์
๋๋ค. ๊ทธ๋ฌ๋ฉด ์ด์ Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๊ฒ ์ต๋๋ค.
#### 2\) ํจํค์ง ์ค์น
ํฐ๋ฏธ๋์ `pip install`์ด๋ผ๋ ๋ช
๋ น์ด ๋ค์ ์ค์นํ๋ ค๋ ํจํค์ง์ ์ด๋ฆ์ธ `seaborn`์ ์
๋ ฅํฉ๋๋ค.
```
pythonpip install seaborn # Seaborn ์ค์น
```
ํ์ด์ฌ/R ๋ฐฐํฌํ์ธ ์๋์ฝ๋ค(Anaconda)๋ก ์์
ํ์๋ ๋ถ๋ค์ ์๋์ ๊ฐ์ด `pip` ๋ช
๋ น์ด ๋์ `conda` ๋ช
๋ น์ด๋ฅผ ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค.
```
pythonconda install seaborn # Seaborn ์ค์น
```
#### 3\) ์ค์น ํ์ธ
์ค์น ๋ช
๋ น์ด๋ฅผ ์คํํ๋ค๋ฉด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์ ๋ก ์ ์ค์น๋์๋์ง ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค. ํจํค์ง์ ์ค์น ์ฌ๋ถ๋ฅผ ์๊ณ ์ถ๋ค๋ฉด ์ค์น๋ ํจํค์ง์ ๋ฒ์ ์ ๋ณด๋ฅผ ํ์ธํ๋ฉด ๋ฉ๋๋ค. ๋ค์ ์ฝ๋๋ฅผ ์
๋ ฅํด์ ์ค์น๋ Seaborn์ ๋ฒ์ ์ ๋ณด๊ฐ ๋ณด์ด๋ฉด Seaborn์ด ์ ์ค์น๋ ๊ฒ์
๋๋ค.
```
pythonimport seaborn as snssns.__version__ # ์ค์น๋ Seaborn ๋ฒ์ ์ ๋ณด ํ์ธ
```
๊ทธ๋ํ๋ฅผ ์ถ๋ ฅํด์ ์ค์น ์ฌ๋ถ๋ฅผ ํ์ธํ ์๋ ์์ต๋๋ค. ์ฝ๋๋ ์๋์ ๊ฐ์ต๋๋ค.
```
python# Seaborn ์ค์น ํ์ธ: ๊ทธ๋ํ ์ถ๋ ฅimport seaborn as snsdf = sns.load_dataset('penguins')sns.pairplot(df, hue='species') # ๋ค๋ฅธ ๊ทธ๋ํ ์ฌ์ฉ ๊ฐ๋ฅ
```
Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น๋ฅผ ์๋ฃํ๋ค๋ฉด ๋ค์์ ๊ธฐ๋ณธ์ ์ธ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
### ๊ธฐ๋ณธ ํ๊ฒฝ ์ค์
๊ธฐ๋ณธ ํ๊ฒฝ ์ค์ ์ ๊ทธ๋ํ ์ ์ญ์ ์ ์ฉ๋๋ ์คํ์ผ๋ง(styling)์
๋๋ค. ์ฝ๋๋ณ ํ๊ฒฝ ์ค์ ์ ๋ํ ์์ธํ ์ค๋ช
์ ํ์ด์ฌ Matplotlib ์ฌ์ฉ๋ฒ(์์ )์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
python# ํจํค์ง ์ํฌํธimport numpy as np # Numpy(๋ํ์ด) ํจํค์ง ์ํฌํธimport pandas as pd # pandas(ํ๋ค์ค) ํจํค์ง ์ํฌํธimport matplotlib.pyplot as plt # Matplotlib(๋งทํ๋กฏ๋ฆฝ) ํจํค์ง์ pyplot ๋ชจ๋์ plt๋ก ์ํฌํธfrom matplotlib import rcParams # ํ๊ธ ํ๊ฒฝ ์ค์ ์ ์ํ rcParams ์ํฌํธimport seaborn as sns # Seaborn(์จ๋ณธ) ํจํค์ง ์ํฌํธimport warnings# ํ๊ธ ํ๊ฒฝ ์ค์ def setting_styles_basic():rcParams['font.family'] = 'Malgun Gothic' # Windows# rcParams['font.family'] = 'AppleGothic' # MacrcParams['axes.unicode_minus'] = False # ํ๊ธ ํฐํธ ์ฌ์ฉ ์, ๋ง์ด๋์ค ๊ธฐํธ๊ฐ ๊นจ์ง๋ ํ์ ๋ฐฉ์งsetting_styles_basic()# ๊ฒฝ๊ณ ์ฐฝ ๋ฌด์warnings.filterwarnings('ignore')
```
Matplotlib์ ์ด์ฉํ์ง ์๊ณ Seaborn์ผ๋ก ํ๊ฒฝ ์ค์ ์ ํ ์๋ ์์ต๋๋ค.
### ์ ์ฒด ์คํ์ผ๋ง
Seaborn์์ ๋ชจ๋ ์คํ์ผ๋ง์ ํ ๋ฒ์ ์ค์ ํ๋ ค๋ฉด `set_theme()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
> - set\_theme: ๊ทธ๋ํ ์ ๋ฐ ๋ฐ ๋งค์ฒด๋ณ ์ค์ผ์ผ(scale), ์์ ํ๋ ํธ ์ง์
๋ค์ ํจ์๋ `set_theme()`์ ์ผ์ ์ญํ ๋ถ๋ดํฉ๋๋ค.
> - set\_style: ๊ทธ๋ํ ์ ๋ฐ ์คํ์ผ ์ง์
> - set\_context: ๋งค์ฒด๋ณ ์ค์ผ์ผ ์ง์
> - set\_palette: ์์ ํ๋ ํธ ์ง์
#### set\_theme
`set_theme()` ํจ์๋ ๊ทธ๋ํ ์ ๋ฐ์ ์ ์ฉ๋๋ ํ
๋ง(theme)๋ฅผ ์ง์ ํ๋ ํจ์์
๋๋ค. `set_theme()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๊ทธ๋ํ ์ ์ญ์ ์คํ์ผ๋ง์ ์ง์ ํ๋ `set_style()` ํจ์์ ์ฌ์ฉํ ๋งค์ฒด์ ์ ํฉํ๋๋ก ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์กฐ์ ํ๋ `set_context()` ํจ์๋ก ํ๋ ์ผ์ ํ ๋ฒ์ ํ ์ ์์ต๋๋ค. ์ฌ์ฉ ์์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythoncustom_params = {"axes.spines.right": False, "axes.spines.top": False}sns.set_theme(context='notebook', # ๋งค์ฒด: paper, talk, posterstyle='darkgrid', # ๊ธฐ๋ณธ ๋ด์ฅ ํ
๋งpalette='deep', # ๊ทธ๋ํ ์font='Malgun Gothic', # ๊ธ๊ผด ์ข
๋ฅfont_scale=1, # ๊ธ๊ผด ํฌ๊ธฐrc=custom_params) # ๊ทธ๋ํ ์ธ๋ถ ์ฌํญ
```
##### context
`context` ํ๋ผ๋ฏธํฐ๋ ์ฌ์ฉํ๋ ๋งค์ฒด์ ์ ํฉํ ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์กฐ์ ํ๋ ์ต์
์
๋๋ค. ์ด ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๊ฐ ๋งค์ฒด์ ์ ํฉํ๊ฒ ๋ผ๋ฒจ๊ณผ ๊ทธ๋ํ์ ํฌ๊ธฐ๋ฅผ ๋ง์ถค ์ค์ ํ ์ ์์ต๋๋ค. ์ ํํ ์ ์๋ ์ต์
์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
> - notebook: ๊ธฐ๋ณธ ์ค์
> - paper: ๋
ผ๋ฌธ, ๋ณด๊ณ ์
> - talk: ํ๋ฆฌ์ ํ
์ด์
> - poster: ํฌ์คํฐ
##### style
`style` ํ๋ผ๋ฏธํฐ๋ Seaborn์ ๊ธฐ๋ณธ ๋ด์ฅ ํ
๋ง(built-in themes)๋ฅผ ์ง์ ํ๋ ์ต์
์
๋๋ค. ๋ด์ฅ ํ
๋ง์๋ ์ด 5๊ฐ์ง ํ
๋ง๊ฐ ์์ต๋๋ค.
> - darkgrid: ํ์ ๋ฐฐ๊ฒฝ์ + ๊ทธ๋ฆฌ๋
> - whitegrid: ํฐ์ ๋ฐฐ๊ฒฝ์ + ๊ทธ๋ฆฌ๋
> - dark: ํ์ ๋ฐฐ๊ฒฝ์
> - white: ํฐ์ ๋ฐฐ๊ฒฝ์
> - ticks: ํฐ์ ๋ฐฐ๊ฒฝ์ + ๋๊ธ
##### palette
`palette` ํ๋ผ๋ฏธํฐ๋ ๊ทธ๋ํ์ ์์ ์ง์ ํ๋ ์ต์
์
๋๋ค. ์ฌ์ฉํ ์ ์๋ ๊ธฐ๋ณธ ์์ ํ๋ ํธ๋ ์ด 6๊ฐ์ง(`deep`, `muted`, `pastel`, `bright`, `dark`, `colorblind`)์
๋๋ค. ํน์ ํ๋ ํธ๋ฅผ ์ ํํ๋ ค๋ฉด `color_palette()` ํจ์๋ฅผ, ์ ํํ ํ๋ ํธ์ ์์์ ํ์ธํ๊ณ ์ถ๋ค๋ฉด `palplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonpalette = sns.color_palette('deep')sns.palplot(palette)
```
##### font, font\_scale
`font`์ `font_scale`์ ๊ฐ๊ฐ ๊ธ๊ผด์ ์ข
๋ฅ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๋ ํ๋ผ๋ฏธํฐ์
๋๋ค. Matplotlib์ `rcParams`์์ `font.family`์ `font.size`๊ฐ ํ๋ ์ผ๊ณผ ๋์ผํ ์ผ์ ์ํํฉ๋๋ค.
```
pythonfrom matplotlib import rcParamsrcParams['font.family'] = 'Malgun Gothic' # WindowsrcParams['font.size'] = 18
```
Matplotlib์ `rcParams`์์์ฒ๋ผ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ ์ ๋ฐ์ ์กฐ์ ํ๊ณ ์ถ๋ค๋ฉด `rc` ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
##### rc
`rc` ํ๋ผ๋ฏธํฐ๋ ์ถ(axes), ๊ทธ๋ฆฌ๋(grid), ๋๊ธ(ticks), ๊ธ๊ผด(font) ๋ฑ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ์ ์ ๋ฐ์ ์กฐ์ ํ๋ ํ๋ผ๋ฏธํฐ์
๋๋ค. `plotting_context()` ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ฌ ๊ทธ๋ํ์ ์ ์ฉ๋๊ณ ์๋ ์ค์ ๊ฐ์ ์ ์ ์์ต๋๋ค. `rc` ํ๋ผ๋ฏธํฐ์์ ์ฌ์ฉํ ์ ์๋ ์ค์ ๊ฐ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.plotting_context()# ๊ฒฐ๊ณผ{'axes.facecolor': 'white','axes.edgecolor': 'black','axes.grid': False,'axes.axisbelow': 'line','axes.labelcolor': 'black','figure.facecolor': 'white','grid.color': '#b0b0b0','grid.linestyle': '-','text.color': 'black','xtick.color': 'black','ytick.color': 'black','xtick.direction': 'out','ytick.direction': 'out','lines.solid_capstyle': <CapStyle.projecting: 'projecting'>,'patch.edgecolor': 'black','patch.force_edgecolor': False,'image.cmap': 'viridis','font.family': ['sans-serif'],'font.sans-serif': ['DejaVu Sans','Bitstream Vera Sans','Computer Modern Sans Serif','Lucida Grande','Verdana','Geneva','Lucid','Arial','Helvetica','Avant Garde','sans-serif'],'xtick.bottom': True,'xtick.top': False,'ytick.left': True,'ytick.right': False,'axes.spines.left': True,'axes.spines.bottom': True,'axes.spines.right': True,'axes.spines.top': True}
```
์ฌ๊ธฐ์ `axes.spines`์ ๊ทธ๋ํ์ ์ถ์ ๋ํ๋ด๊ฑฐ๋ ์จ๊ธฐ๋ ์ต์
์
๋๋ค. ๋ฐ๋ก ์ค์ ํ์ง ์์ผ๋ฉด Seaborn์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์(top), ์๋(bottom), ์ผํธ(left), ์ค๋ฅธํธ(right) ์ด 4๊ฐ์ ์ถ์ ๋ณด์ฌ์ค๋๋ค. ๋ง์ผ ์์ชฝ ์ถ๊ณผ ์ค๋ฅธ์ชฝ ์ถ์ ์จ๊ธฐ๊ณ ์ถ๋ค๋ฉด `despine()` ํจ์๋ฅผ ์ด์ฉํด๋ ๋ฉ๋๋ค. `despine()` ํจ์๋ ๋ฐ๋์ ๊ทธ๋ํ ํจ์ ๋ค์์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
```
pythonsns.countplot(...)sns.despine() # ์, ์ค๋ฅธ์ชฝ ์ถ ์จ๊ธฐ๊ธฐ
```
๋ง์ผ ํน์ ์ถ์ ์จ๊ธฐ๊ณ ์ถ๋ค๋ฉด ์๋์ ๊ฐ์ด ์จ๊ธฐ๊ณ ์ถ์ ๋ฐฉํฅ์ ํ๋ผ๋ฏธํฐ์ `True` ๊ฐ์ ์ ๋ฌํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.countplot(...)sns.despine(left=True, bottom=True) # ์ผํธ, ์๋ํธ ์ถ๋ ๋ชจ๋ ์จ๊ธฐ๊ธฐ
```
#### set\_style
`set_style()` ํจ์๋ก๋ ๊ทธ๋ํ ์ ๋ฐ์ ์ ์ฉ๋ ํ
๋ง์ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ์ ์กฐ์ ํ ์ ์์ต๋๋ค.
```
pythonrc={'grid.color': '.5', 'grid.linestyle': ':'}sns.set_style('whitegrid', rc=None)
```
#### set\_context
`set_context()` ํจ์๋ก๋ ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์ง์ ํ ์ ์์ต๋๋ค.
```
pythonsns.set_context('notebook', font_scale=1.25, rc={'grid.color': '.6'})
```
#### set\_palette
`set_palette()` ํจ์๋ก๋ ๊ทธ๋ํ์ ์์ ํ๋ ํธ๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
```
pythonsns.set_palatte('colorblind')
```
### ๊ทธ๋ํ๋ณ ์คํ์ผ๋ง
๊ทธ๋ํ๋ณ ์คํ์ผ๋ง์ ํ๋ ค๋ฉด `set()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
#### ์ถ ๋ฒ์ ์ ํํ๊ธฐ: xlim, ylim
Seaborn์์ x์ถ๊ณผ y์ถ์ ๋ฒ์๋ฅผ ์ค์ ํ๋ ค๋ฉด `xlim`, `ylim` ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.countplot(...).set(xlim=(1, 10), ylim=(0, 20))
```
#### ์ถ ๋ผ๋ฒจ ์จ๊ธฐ๊ธฐ: xlabel, ylabel
Seaborn์์ ์ถ์ ์๋ ๋ผ๋ฒจ์ ์จ๊ธฐ๋ ค๋ฉด `xlabel`, `ylabel` ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonax = sns.heatmap(...)ax.set(xlabel="", ylabel="")
```
#### ์ถ ์์น ๋ฐ๊พธ๊ธฐ
์ถ ์์น๋ฅผ ์กฐ์ ํ๋ ค๋ฉด `ax.axis.tick_top` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonax = sns.heatmap(...)ax.xaxis.tick_top() # x์ถ ์๋์์ ์๋ก ์ฎ๊ธฐ๊ธฐax.yaxis.tick_left # y์ถ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ฎ๊ธฐ๊ธฐ
```
#### ๊ทธ๋ํ ํฌ๊ธฐ ์กฐ์ ํ๊ธฐ
Seaborn์์ ๊ฐ๋ณ ๊ทธ๋ํ์ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ ค๋ฉด `rc` ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.barplot(...)sns.set(rc={'figure.figsize':(10,7)})
```
์ค์น์ ๊ธฐ๋ณธ ํ๊ฒฝ ์ค์ ์ ๋ชจ๋ ๋ง์ณค๋ค๋ฉด ์ด์ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉ(loading)ํด์ค๊ฒ ์ต๋๋ค.
### ๋ฐ์ดํฐ
Seaborn์์๋ ๋ฐ์ดํฐ๋ฅผ ์ธ๋ถ์์ ๊ฐ์ ธ์ฌ ์๋ ์๊ณ , ๋ด์ฅ ๋ฐ์ดํฐ(built-in data)๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
#### 1\) ๋ฐ์ดํฐ ์ ํ
##### ์ธ๋ถ ๋ฐ์ดํฐ
์ธ๋ถ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ฉํ๋ ค๋ฉด pandas๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. CSV ํ์ผ๊ณผ ์์
ํ์ผ์ DataFrame ๊ฐ์ฒด๋ก ๋ถ๋ฌ์ค๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonimport pandas as pddf = pd.read_csv('data.csv') # CSV ํ์ผ ๊ฐ์ ธ์ค๊ธฐ# ๋๋df = pd.read_excel('data.xlsx') # ์์
ํ์ผ ๊ฐ์ ธ์ค๊ธฐ
```
pandas์์ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉํ๋ ์์ธํ ๋ฐฉ๋ฒ์ [Python pandas ๋ฐ์ดํฐ ์์ฑ, ๋ก๋ฉ๊ณผ ์ ์ฅ, ์์ธ ๊ด๋ฆฌํ๋ ๋ฒ](https://www.snugarchive.com/blog/python-pandas-guide-1/)์์ '๋ก๋ฉ ๋ฐ ์ ์ฅ' ํธ์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฌ๊ธฐ์๋ Seaborn์ ๋ด์ฅ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ฒ ์ต๋๋ค.
##### ๋ด์ฅ ๋ฐ์ดํฐ
Seaborn์๋ ๋ค์ํ ๋ด์ฅ ๋ฐ์ดํฐ์
์ด ์์ต๋๋ค. ํจํค์ง ๋ด์ ์ด๋ค ๋ด์ฅ ๋ฐ์ดํฐ์
์ด ์๋์ง ์์๋ณด๋ ค๋ฉด `get_dataset_names()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.get_dataset_names()# ๊ฒฐ๊ณผ['anagrams', 'anscombe', 'attention', 'brain_networks', 'car_crashes', 'diamonds','dots', 'exercise', 'flights', 'fmri', 'gammas', 'geyser', 'iris', 'mpg','penguins', 'planets', 'taxis', 'tips', 'titanic']
```
์ด ๋ฐ์ดํฐ์
์ค์์ ๋ช ๊ฐ์ง๋ฅผ ์ ํํด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
#### 2\) ๋ฐ์ดํฐ ๋ก๋ฉ
Seborn์ ๋ด์ฅ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉํ๋ ค๋ฉด `load_dataset()` ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. pandas๋ฅผ ์ด์ฉํด ๊ฐ์ ธ์จ ์ธ๋ถ ๋ฐ์ดํฐ์ ๋ง์ฐฌ๊ฐ์ง๋ก `load_dataset()` ํจ์๋ก ๋ถ๋ฌ์จ ๋ฐ์ดํฐ ํ์๋ DataFrame ๊ฐ์ฒด์
๋๋ค. ์ฌ๊ธฐ์๋ ๋ค์ ๋ฐ์ดํฐ์
์ ๋ถ๋ฌ์ ๋ณด๊ฒ ์ต๋๋ค.
```
pythondf_titanic = sns.load_dataset('titanic') # ํ์ดํ๋ํธ ๋ฐ์ดํฐdf_iris = sns.load_dataset('iris') # ๋ถ๊ฝ ๋ฐ์ดํฐdf_penguins = sns.load_dataset('penguins') # ํญ๊ท ๋ฐ์ดํฐdf_tips = sns.load_dataset('tips') # ํ ๋ฐ์ดํฐdf_diamonds = sns.load_dataset('diamonds') # ๋ค์ด์๋ชฌ๋ ๋ฐ์ดํฐdf_planets = sns.load_dataset('planets') # ํ์ฑ ๋ฐ์ดํฐ
```
#### 3\) ๋ฐ์ดํฐ ๊ตฌ์กฐ ํ์
๋ฐ์ดํฐ์
์ด ์ ์ค๋น๋์๋ค๋ฉด ๋ฐ์ดํฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ๋จํ ์ดํด๋ณด๊ฒ ์ต๋๋ค. pandas์์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ๋ ์์ธํ ๋ฐฉ๋ฒ์ ์๊ณ ์ถ์ผ์๋ค๋ฉด [Python pandas ๋ฐ์ดํฐ ํ์ธ, ์ ๋ ฌ, ์ ํํ๋ ๋ฒ](https://www.snugarchive.com/blog/python-pandas-guide-2/)์์ "๋ฐ์ดํฐ ํ์ธ" ๋ถ๋ถ์ ์ฐธ์กฐํด ์ฃผ์ธ์.
```
pythondf.shape # DataFrame์ ์ด๊ณผ ํ์ ๊ฐ์ ์ถ๋ ฅํ๊ธฐdf.head() # DataFrame์ ์ฒซ ๋ถ๋ถ ์ถ๋ ฅํ๊ธฐdf['class'] # ์ด ์ด๋ฆ์ด 'class'์ธ ๋ถ๋ถ์ ๋ชจ๋ ๋ฐ์ดํฐ ๋ ์ฝ๋ ๊ฐ์ ธ์ค๊ธฐ
```
๋ฐ์ดํฐ์
์ด ์ ์ค๋น๋์๋ค๋ฉด ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์๊ฐํ๋ฅผ ํด๋ณด๊ฒ ์ต๋๋ค. ๊ธฐ๋ณธํธ์์ ์๊ฐํํ ๋ฐ์ดํฐ๋ 1์ฐจ์ ๋ฐ์ดํฐ์
๋๋ค. 1์ฐจ์ ๋ฐ์ดํฐ๋ ์์ฑ(attribute)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ์
๋๋ค. Numpy ๋ฐฐ์ด์์ ์์๋ฅผ ํ ์ค๋ก ๋ํ๋ผ ์ ์๋ ๋ฐ์ดํฐ, ์์
์์ ์ด(columns)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ, ๋
๋ฆฝ๋ณ์(independent variable) ๋๋ ๋ณ๋(variate)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ๋ผ๊ณ ๋ ๋ณผ ์ ์์ต๋๋ค.
1์ฐจ์ ๋ฐ์ดํฐ๋ ์์นํ๊ณผ ๋ฒ์ฃผํ์ผ๋ก ๋๋ฉ๋๋ค. ์์นํ์ ๋ณ์๊ฐ ์ค์ซ๊ฐ์ธ ์ฐ์์ ๋ณ์(continous variables)์ ์ ์ซ๊ฐ์ธ ์ด์ฐ์ ๋ณ์(discrete variables)์ธ ๋ฐ์ดํฐ์
๋๋ค. ๋ฒ์ฃผํ์ ๋ณ์๊ฐ ์นดํ
๊ณ ๋ฆฌ(category)์ฒ๋ผ ๋ถ๋ฅ๋ ์ง์ ๋ณ์(qualitative variables)๋ฅผ ์๋ฏธํฉ๋๋ค. ๊ทธ๋ผ 1์ฐจ์ ๋ฒ์ฃผํ ๋ฐ์ดํฐ๋ถํฐ ์๊ฐํํด ๋ณด๊ฒ ์ต๋๋ค.
## 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ๋ฒ์ฃผํ
์ผ๋ณ๋ ๋ฒ์ฃผํ ์๋ฃ๋ฅผ ์๊ฐํํ๋ ๋ฐ๋ ๋ง๋๊ทธ๋ํ(bar graph)์ ํ์ด ์ฐจํธ(pie chart)๋ฅผ ์ด์ฉํฉ๋๋ค. ๋ค๋ง, Seaborn์๋ ํ์ด ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ธฐ๋ฅ์ด ์์ต๋๋ค. ํ์ด ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด Matplotlib์ ์ด์ฉํด์ผ ํฉ๋๋ค. ์์ธํ ๋ฐฉ๋ฒ์ ํ์ด์ฌ Matplotlib ์ฌ์ฉ๋ฒ(์์ )์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฌ๊ธฐ์๋ Seaborn์ผ๋ก ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
### 1\) ๋น๋ ๋ง๋๊ทธ๋ํ: countplot()
Seaborn์ผ๋ก ๋ฒ์ฃผํ ๋ฐ์ดํฐ์ ๋ํ ๋น๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ํจ์๋ `countplot()`์
๋๋ค. ์ด ํจ์๋ ๊ฐ ๋ฒ์ฃผ์ ์ํ๋ ๋ฐ์ดํฐ์ ๋น๋(๊ฐ์)๋ฅผ ๋ง๋์ ๋์ด๋ก ํ์ํฉ๋๋ค. ๋จผ์ ์์ง ๋น๋ ๋ง๋๊ทธ๋ํ๋ถํฐ ๊ทธ๋ ค๋ณด๊ฒ ์ต๋๋ค.
#### ์์ง ๋น๋ ๋ง๋๊ทธ๋ํ
##### ๊ธฐ๋ณธ
Seaborn์ผ๋ก ์์ง ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ธฐ๋ณธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.countplot(df_titanic['class']) # ์ฝ๋1sns.countplot(x=df_titanic['class']) # ์ฝ๋1sns.countplot(x='class', data=df_titanic) # ์ฝ๋1
```
์ฝ๋1์ ์๋์ ๊ฐ์ด ์ต์
์ ์ถ๊ฐํ ์๋ ์์ต๋๋ค.
> - color: ๋ง๋ ์ ์ง์
> - edgecolor: ๋ง๋ ํ
๋๋ฆฌ์ ์ง์
> - palette: ๊ทธ๋ํ ์ ์ง์
> - alpha: ๊ทธ๋ํ ํฌ๋ช
๋ ์ง์
> - linewidth: ๊ทธ๋ํ ๊ตต๊ธฐ ์ง์
`palette`์ ๋ค์ํ ์ต์
์ [Seaborn ๊ณต์ ํํ์ด์ง color palette](https://seaborn.pydata.org/tutorial/color_palettes.html)์์ ํ์ธํ์ค ์ ์์ต๋๋ค.
```
pythonsns.countplot(x='class', data=df_titanic, color='skyblue') # ์ฝ๋2sns.countplot(x='class', data=df_titanic, palette='Set3') # ์ฝ๋3sns.countplot(x='class', data=df_titanic, # ์ฝ๋4facecolor=(0, 0, 0, 0),linewidth=5,edgecolor=sns.color_palette('dark', 3))
```
์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.




countplot ํจ์๋ก ๊ทธ๋ฆฐ ์์ง ๋ง๋๊ทธ๋ํ
`countplot()` ํจ์ ์ธ์๋ `catplot()` ํจ์๋ฅผ ์ด์ฉํ ์๋ ์์ต๋๋ค. `catplot()` ํจ์๋ ์์นํ ๋ณ์์ ๋ฒ์ฃผํ ๋ณ์์ ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ ํจ์์ด์ง๋ง, `kind='count'` ์ต์
์ ์ถ๊ฐํ๋ฉด ๋น๋๋ฅผ ๋ํ๋ด๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆด ์ ์์ต๋๋ค (์ฝ๋1๊ณผ ๋์ผ).
```
pythonsns.catplot(x='class', kind='count', data=df_titanic) # ์ฝ๋1
```




catplot() ํจ์๋ก ๊ทธ๋ฆฐ ์์ง ๋ง๋๊ทธ๋ํ
##### ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
๋ง์ผ ๋ง๋๊ทธ๋ํ๋ฅผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ๋ค๋ฉด `order` ํ๋ผ๋ฏธํฐ์ `df.value_counts().index` ์ฝ๋๋ฅผ ๋ํด์ฃผ๋ฉด ๋ฉ๋๋ค. `df.value_counts().index`๋ ๋ณ์์ ๊ฐ์ ๋น๋๊ฐ ๋์ ์์ผ๋ก ์ ๋ ฌํด์ค๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.countplot(x='class', data=df_titanic,order=df_titanic['class'].value_counts().index)
```




๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ ์์ง ๋ง๋๊ทธ๋ํ
##### ์์ฝ๊ฐ ํ์
๊ฐ ๋ง๋ ์์ ์์ฝ๊ฐ์ ์ซ์๋ก ํ์ํ๊ณ ์ถ๋ค๋ฉด ๊ทธ๋ํ๋ฅผ ๋ณ์์ ํ ๋นํ ๋ค `ax.bar_label(ax.containers[0])` ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค.
```
pythonax = sns.countplot(df_titanic['class'])ax.bar_label(ax.containers[0])
```




์์ฝ๊ฐ์ ํ์ํ ์์ง ๋ง๋๊ทธ๋ํ
##### ์๊น ๊ฐ์กฐ
ํน์ ๋ง๋์ ์๊น์ ๊ฐ์กฐํ ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๊ฐ์ฅ ๋น๋์๊ฐ ๋์ ๋ง๋๋ง ๋ค๋ฅธ ์์ผ๋ก ํ์ํ๊ณ ์ถ๋ค๋ฉด `numpy`์ `barplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonvalues = np.array(df_titanic['class'].value_counts())idx = np.array(df_titanic['class'].value_counts().index)palette = ['skyblue' if (x == max(values)) else 'lightgrey' for x in values]sns.barplot(x=idx, y=values, palette=palette)
```




ํน์ ๋ง๋๋ฅผ ๋ค๋ฅธ ์์ผ๋ก ๊ฐ์กฐํ ์์ง ๋ง๋๊ทธ๋ํ
#### ์ํ ๋น๋ ๋ง๋๊ทธ๋ํ
Seaborn์ผ๋ก ์ผ๋ณ๋ ๋ฒ์ฃผํ ์๋ฃ์ ๋ํ ๊ฐ๋ก ๋น๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด `countplot()` ํจ์์ x ๋งค๊ฐ๋ณ์ ๋์ y ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.countplot(y='class', data=df_titanic) # ์ฝ๋1sns.catplot(y='class', kind='count', palette='ch:.25', data=df_titanic) # ์ฝ๋2
```




countplot() ํจ์๋ก ๊ทธ๋ฆฐ ์ํ ๋ง๋๊ทธ๋ํ
์ง๊ธ๊น์ง ์ผ๋ณ๋ ๋ฒ์ฃผํ ๋ฐ์ดํฐ๋ฅผ ๋ง๋๊ทธ๋ํ๋ก ์๊ฐํํ๋ ๋ฒ์ ์ดํด๋ณด์์ต๋๋ค. ์ด๋ฒ์๋ ์ผ๋ณ๋ ์์นํ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
### 2\) ์์ ๊ทธ๋ฆผ
์์ ๊ทธ๋ฆผ(๋์์ด: box plot, ๋ฐ์ค ํ๋กฏ, ๋ฐ์ค ๊ทธ๋ํ, ์์ ๊ทธ๋ํ)์ ๋ฐ์ดํฐ์ 5๊ฐ์ง ํต๊ณ๋(์ต์๊ฐ, ์ 1 ์ฌ๋ถ์, ์ 2์ฌ๋ถ์, ์ 3์ฌ๋ถ์, ์ต๋๊ฐ)์ ๋ํ๋ด๋ ๊ทธ๋ํ์
๋๋ค. ์์๊ทธ๋ฆผ์ ๋ฐ์ดํฐ์ ์ ์ฒด์ ์ธ ๋ถํฌ์ ์ด์์น๋ฅผ ํ์ธํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
#### boxplot()
์์๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๋ ค๋ฉด `boxplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.boxplot(data=df_iris, x='sepal_length')
```




boxplot ํจ์๋ก ๊ทธ๋ฆฐ ์์ ์์ผ ๊ทธ๋ฆผ
`catplot()` ํจ์์ `kind='box'` ์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
```
pythonsns.catplot(data=df_iris, x='sepal_length', kind='box')
```
#### boxenplot()
๋ฐ์ดํฐ์
์ ๋ฐ์ดํฐ ๋ฒ์๊ฐ ํด ๊ฒฝ์ฐ์๋ `boxenplot()`์ ์ฌ์ฉํ ์ ์์ต๋๋ค. `boxenplot()`์ ๋ฐ์ดํฐ๋ฅผ ๋ ๋ง์ ๋ถ์๋ก ๋๋์ด ํฌ๊ธฐ๊ฐ ํฐ ๋ฐ์ดํฐ์
์ ๋ฒ์ฃผ๋ฅผ ์์๊ทธ๋ฆผ์ผ๋ก ํํํด ์ค๋๋ค.
```
pythonsns.boxenplot(data=df_diamonds, x='price')
```




boxenplot() ํจ์๋ก ๊ทธ๋ฆฐ ์์๊ทธ๋ฆผ
`catplot()` ํจ์์ `kind='boxen'` ์ต์
์ ์ฌ์ฉํด๋ ๋ฉ๋๋ค.
```
pythonsns.catplot(data=df_diamonds, x='price', kind='boxen')
```
#### violinplot()
์์ ๊ทธ๋ฆผ๊ณผ ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ํฉ์ณ์ ๊ทธ๋ฆฌ๋ ค๋ฉด `violinplot()`์ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.violinplot(data=df_iris, x='sepal_length')
```




violinplot() ํจ์๋ก ๊ทธ๋ฆฐ ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ
```
pythonsns.catplot(data=df_iris, x='sepal_length', kind='violin')
```
์ ๊ทธ๋ํ์์ ๊ฐ์ด๋ฐ ํฐ์ ์ ์ ์ค์๊ฐ(median)์ ๋ํ๋
๋๋ค. ๋ํ, ํฐ์ ์ ์ ๋๋ฌ์ผ ๋๊บผ์ด ์ ์ ์ฌ๋ถ์ ๋ฒ์๋ฅผ, ๋๊บผ์ด ์ ์์ ์ ๋์ผ๋ก ์ด์ด์ง๋ ์์ ์ ์ 95% ์ ๋ขฐ ๊ตฌ๊ฐ์ ๋ํ๋
๋๋ค.
## 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ์์นํ
์์นํ ๋ฐ์ดํฐ๋ ๋ถํฌ๋ฅผ ๋ณด๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
### 1\) ์ ๊ทธ๋ํ: stripplot(), swarmplot()
์ ๊ทธ๋ํ(๋์์ด: dot graph, strip chart, ์ ๋ํ)๋ ๋ฐ์ดํฐ์ ์ค์ ์์น๋ฅผ ์ (dots)์ผ๋ก ๋ณด์ฌ์ค๋๋ค. ์ ๊ทธ๋ํ๋ ๋ฐ์ดํฐ์ ์ค์ ์์น๋ฅผ ํ์
ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค. Seaborn์ผ๋ก ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด `stripplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.stripplot(data=df_iris, x='sepal_length')# ๋๋sns.stripplot(x=df_iris['sepal_length'])
```




stripplot() ํจ์๋ก ๊ทธ๋ฆฐ ์ ๊ทธ๋ํ
`catplot()` ํจ์์ `kind='strip'` ์ต์
์ ์ถ๊ฐํด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
```
pythonsns.catplot(data=df_iris, x='sepal_length', kind='strip')
```
๋ค๋ง ์ ๊ทธ๋ํ์ ๊ฒฝ์ฐ ํํํ ๋ฐ์ดํฐ ํฌ์ธํธ๋ค์ด ๋ง์์ง๋ฉด ์ ๋ค์ด ๊ฒน์ณ ๋ณด์ผ ์ ์์ต๋๋ค. ์ด๋๋ `swarmplot()`์ ์ด์ฉํด ์๋ฃ๋ฅผ ํฉํธ๋ ค์(jittering) ์ ์ฌ์ด์ ๊ฐ๊ฒฉ์ ์กฐ์ ํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.swarmplot(data=df_iris, x='sepal_length')
```




swarmplot() ํจ์๋ก ํํธ๋ ค ๊ทธ๋ฆฐ ์ ๊ทธ๋ํ
### 2\) ์ ๋ถ๊ทธ๋ํ: rugplot()
์ ๋ถ๊ทธ๋ํ(rug plot) ๋๋ ๋ฌ๊ทธ ํ๋กฏ์ ๋ชจ๋ ๋ฐ์ดํฐ ํฌ์ธํธ๋ฅผ ์ถ ์์ ์์ ์ ๋ถ(rug)์ผ๋ก ๋ํ๋ธ ๊ทธ๋ํ์
๋๋ค. ์ ๋ถ ๊ทธ๋ํ์ ๊ฐ ์ ๋ถ์ ์ค์ ๋ฐ์ดํฐ๋ค์ ์์น๋ฅผ ๋ณด์ฌ์ค๋๋ค. ์ ๋ถ๋ค์ด ์ด์ดํ ์์์๋ก ๋ฐ์ดํฐ๊ฐ ๋ฐ์ง๋์ด ์์์ ์๋ฏธํฉ๋๋ค. ์ฃผ๋ณ ๋ถํฌ(marginal distribution)์ ๋ํ๋ด๋ฉฐ ์ฃผ๋ก ๋ค๋ฅธ ๊ทธ๋ํ์ ํจ๊ป ์ฌ์ฉ๋ฉ๋๋ค.
๋ฌ๊ทธ ํ๋กฏ์ ๊ทธ๋ฆฌ๋ ค๋ฉด `rugplot()` ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. `displot()` ํจ์์ `rug=True` ์ต์
์ ์ฃผ์ด๋ ๋ฉ๋๋ค. ๋ฌ๊ทธ๋ฅผ ์ธ๋ถ์ ์ผ๋ก ์กฐ์ ํด์ผ ํ ๋๋ `displot()` ํจ์๋ณด๋ค๋ `rugplot()` ํจ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
> - height: ์ ๋ถ ๊ธธ์ด ์ง์
> - clip\_on: ์ ๋ถ ์ถ ๋ฐ์ ๊ทธ๋ฆฌ๊ธฐ ์ง์
> - lw: ์ ๋ถ ์๊ธฐ ์ง์
> - alpha: ์ ๋ถ ํฌ๋ช
๋ ์ง์
```
python# ์ฝ๋1: ์ ๋ถ ๊ทธ๋ฆฌ๊ธฐsns.displot(data=df_tips, x='total_bill', rug=True)# ์ฝ๋2: ๋์ด ์กฐ์ ํ ์ ๋ถ ๊ทธ๋ฆฌ๊ธฐsns.rugplot(data=df_tips, x='total_bill', height=.1)# ์ฝ๋3: ์ ๋ถ ์ถ ๋ฐ์ ๊ทธ๋ฆฌ๊ธฐsns.rugplot(data=df_tips, x='total_bill', height=-.02, clip_on=False)# ์ฝ๋4: ์ ๋ถ ์๊ฒ ๊ทธ๋ฆฌ๊ธฐ & ํฌ๋ช
๋ ์ถ๊ฐsns.rugplot(data=df_diamonds, x='carat', lw=1, alpha=.005)
```




rugplot() ํจ์๋ก ๊ทธ๋ฆฐ ๋ค์ํ ์ ๋ถ๊ทธ๋ํ
๋ฐ์ดํฐ๊ฐ ๋ง์์ง ๊ฒฝ์ฐ ์ ๊ทธ๋ํ ๋๋ ์ ๋ถ๊ทธ๋ํ๋ง์ผ๋ก ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ํ๋์ ํ์
ํ๊ธฐ ์ด๋ ต์ต๋๋ค. ๋ฐ์ดํฐ ํฌ์ธํธ๊ฐ ์๋ก ๊ฒน์ณ ๋ณด์ด๊ธฐ ๋๋ฌธ์
๋๋ค. ์ด๋ ์ฌ์ฉํ๋ฉด ์ข์ ๋๊ตฌ๊ฐ ํ์คํ ๊ทธ๋จ์
๋๋ค.
### 3\) ํ์คํ ๊ทธ๋จ: histplot()
ํ์คํ ๊ทธ๋จ(histogram)์ ์์นํ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ๊ตฌ๊ฐ๋ณ ๋น๋์๋ก ํํํ ๊ทธ๋ํ์
๋๋ค. ์ฆ, ๋ฐ์ดํฐ๋ฅผ ๋ช ๊ฐ์ ๊ตฌ๊ฐ์ผ๋ก ๋๋ ํ ๊ฐ ๊ตฌ๊ฐ์ ํฌํจ๋ ๋ฐ์ดํฐ์ ๊ฐ์ ๋๋ ๋์(frequency)๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ๋ํ๋ธ ๊ฒ์
๋๋ค. ๋ฐ์ดํฐ๊ฐ ๊ตฌ๊ฐ์ผ๋ก ์ฒ๋ฆฌ๋๊ธฐ ๋๋ฌธ์, ๋ฒ์๊ฐ ๋์ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ํ์
ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
#### ๊ธฐ๋ณธ
Seaborn์์ ๋ณ์๊ฐ 1๊ฐ์ธ ๋จ๋ณ๋ ํ์คํ ๊ทธ๋จ์ ๋ง๋ค๋ ค๋ฉด `histplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์ปค๋๋ฐ๋์ถ์ (Kernel Density Estimation, KDE) ๋ฐฉ๋ฒ์ผ๋ก ์ค๋ฌด๋ฉ(smoothing)ํ ํ์คํ ๊ทธ๋จ์ ํ๋ฅ ๋ฐ๋ํจ์(Probability Density Function, PDF)๋ฅผ ํจ๊ป ๊ทธ๋ฆฌ๊ณ ์ถ๋ค๋ฉด `kde=True` ์ต์
์ ์ฃผ๋ฉด ๋ฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.histplot(df_penguins, x='flipper_length_mm') # ์ฝ๋1sns.histplot(df_penguins['flipper_length_mm'], kde=True) # ์ฝ๋2
```
`displot()` ํจ์๋ฅผ ์ด์ฉํด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค. `displot()` ํจ์์ ์ด๊ธฐ ๊ธฐ๋ณธ ์ค์ ์ `kind='hist'`์
๋๋ค. `displot()` ํจ์์ `kind` ์ต์
์ ๋ฐ๋ก ์ง์ ํ์ง ์์ผ๋ฉด ํ์คํ ๊ทธ๋จ์ ๊ทธ๋ฆฝ๋๋ค.
```
pythonsns.displot(df_penguins, x='flipper_length_mm') # ์ฝ๋1sns.displot(df_penguins['flipper_length_mm'], kde=True) # ์ฝ๋2
```




histplot() ํจ์๋ก ๋ง๋ ๋จ๋ณ๋ ํ์คํ ๊ทธ๋จ
```
pythonsns.displot(df_diamonds, x='carat', kde=True)
```




ํ์คํ ๊ทธ๋จ๊ณผ ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์
#### ํน์ ์กฐ๊ฑด
pandas๋ฅผ ์ด์ฉํ๋ฉด ๋ฐ์ดํฐ๋ฅผ ์กฐ๊ฑด๋ณ๋ก ์ ํํ ์ ์์ต๋๋ค. ๋ค์ ์ฝ๋๋ 'species'๊ฐ 'Adelie'์ธ ํญ๊ท์ 'flipper\_length\_mm'๋ฅผ ๊ด์ธกํ ๊ฐ์ ๋ํด ํ์คํ ๊ทธ๋จ์ ๊ทธ๋ฆฌ๋ ์์ ์
๋๋ค.
```
pythonsns.histplot(df_penguins[df_penguins['species'] == 'Adelie']['flipper_length_mm'])
```
#### ๋ฑ๊ธ ์์ ๋ฑ๊ธ ํญ: bins, binwidth
ํ์คํ ๊ทธ๋จ์์๋ ๋ฑ๊ธ์ ์ ๋๋ ๋ฑ๊ธ์ ํญ์ ๋ฐ๋ผ ๊ทธ๋ํ์ ๋ชจ์์ด ๋ฌ๋ผ์ง๋๋ค. ๋ฑ๊ธ์ ์๋ `bins` ์ต์
์ผ๋ก, ๋ฑ๊ธํญ์ `binwidth` ์ต์
์ผ๋ก ์ง์ ํฉ๋๋ค.
```
pythonsns.histplot(df_penguins, x='flipper_length_mm', bins=10) # ๋ฑ๊ธ์: 10sns.histplot(df_penguins, x='flipper_length_mm', binwidth=3) # ๋ฑ๊ธํญ: 3
```




๊ตฌ๊ฐ์ ์์ ๊ตฌ๊ฐ์ ํญ์ ์กฐ์ ํ ํ์คํ ๊ทธ๋จ
#### ๋ฑ๊ธ๋ช
, ๋ฑ๊ธ๋ช
์์น, ๋ฑ๊ธ ์ฌ์ด ๊ณต๊ฐ: bins=๋ฆฌ์คํธ, discrete=True, shrink
ํ์คํ ๊ทธ๋จ์ ์ฃผ๋ก ์ฐ์์ ์๋ฃ๋ฅผ ์๊ฐํํ ๋ ์ฐ์ด์ง๋ง, ์ข
์ข
์ด์ฐ์ ์๋ฃ(discrete data)๋ฅผ ์๊ฐํํ๋ ๋ฐ๋ ์ฌ์ฉํฉ๋๋ค. ์ด์ฐ์ ์๋ฃ๋ ๋ง๋๊ทธ๋ํ๋ก ํํํฉ๋๋ค. ๋จ, ํ์คํ ๊ทธ๋จ์ ๋ง๋๊ทธ๋ํ๋ก ๋ณํํ ๋๋ ๋ค์๊ณผ ๊ฐ์ ์กฐ์ ์ด ํ์ํฉ๋๋ค.
> - ๋ฑ๊ธ ๋ช
์
> - ๋ฑ๊ธ๋ช
์ด ๋ง๋์ ์ค์์ ์ค๋๋ก ์์น
> - ๋ณ์๊ฐ ์ฐ์์ ์ด์ง ์๊ณ ์ด์ฐ์ ์ด๋ผ๋ ๊ฒ์ ์๋ ค์ฃผ๊ธฐ ์ํด ๋ฑ๊ธ๊ณผ ๋ฑ๊ธ ์ฌ์ด์ ์ฌ์ ๋๊ธฐ
์ด๋ฅผ ๋์์ฃผ๋ ์ต์
์ด ๊ฐ๊ฐ `bins=๋ฆฌ์คํธ`, `discrete=True`, `shrink`์
๋๋ค. ๊ฐ ์ต์
์ด ์ํํ๋ ์ผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
> - bins=๋ฆฌ์คํธ: ๊ทธ๋ํ์ x์ถ์ ๋ช
์ํ ๋ฑ๊ธ์ ์ง์ ์ง์
> - discrete=True: ๊ฐ ๋ฑ๊ธ์ด ๋ง๋ ์ค์์ ์ค๋๋ก ์์น
> - shrink: ๊ฐ ๋ง๋ ์ฌ์ด์ ๊ณต๊ฐ์ ๋ง๋ จ
์์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
python# ์ฝ๋1: ๊ธฐ๋ณธ ์ค์ (๋๊ธ์ ์์น ์กฐ์ ํ์)sns.histplot(df_tips, x='size')# ์ฝ๋2: ๋ฑ๊ธ ํ๊ณ ์ง์ sns.histplot(df_tips, x='size', bins=[1, 2, 3, 4, 5, 6, 7])# ์ฝ๋3: ๊ฐ ๋ฑ๊ธ๋ช
์ ๋ง๋๊ทธ๋ํ ์ค์์ผ๋ก ์ด๋sns.histplot(df_tips, x='size', discrete=True)# ์ฝ๋4: ๋ฑ๊ธ๋ช
์ค์์ผ๋ก ์ด๋ํ ๋ค ๋ฑ๊ธ ๊ฐ ๊ณต๊ฐ ๋ง๋ จsns.histplot(df_tips, x='size', discrete=True, shrink=.8)
```




๋ฑ๊ธ๋ช
๊ณผ ๋ฑ๊ธ๋ช
์ ์์น, ๋ฑ๊ธ ์ฌ์ด์ ๊ณต๊ฐ์ ์กฐ์ ํ ํ์คํ ๊ทธ๋จ
๋จ, ํ์คํ ๊ทธ๋จ์ ๋ฐ์ดํฐ์ ์ฐ์์ ํน์ฑ์ ์จ์ ํ ํํํ์ง ๋ชปํ๋ ๋จ์ ์ด ์์ต๋๋ค. ๊ตฌ๊ฐ์ ํฌ๊ธฐ์ ์์์ ์ ๋ฐ๋ผ ๋ถํฌ์ ๋ชจ์์ด ํฌ๊ฒ ๋ฌ๋ผ์ง ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ฐ์ํ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ๋ถ๋๋ฌ์ด ๊ณก์ ์ผ๋ก ํํํ๋ ๋ฐฉ๋ฒ์ด ์์๊น์?
### 4\) ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์: kdeplot()
์ปค๋๋ฐ๋์ถ์ (Kernel density estimation)์ ์ฃผ์ด์ง ๋ฐ์ดํฐ ํฌ์ธํธ๋ค์ ๋ฐํ์ผ๋ก ์ ์ฒด ๋ฐ์ดํฐ์ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ๋ถ๋๋ฌ์ด ๊ณก์ ์ผ๋ก ์ถ์ ํ๋ ๊ธฐ๋ฒ์
๋๋ค. ์์นํ ๋ฐ์ดํฐ๋ฅผ ์ด์ฐ์ ์ผ๋ก ํํํ๋ ํ์คํ ๊ทธ๋จ ๋์ ๋งค๋๋ฌ์ด ๊ณก์ ์ผ๋ก ํํํ๊ณ ์ถ๋ค๋ฉด ์ปค๋ ๋ฐ๋ ํจ์(Kernel density function)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
#### ๊ธฐ๋ณธ
`kdeplot()` ํจ์๋ ๋จ๋ณ๋ ๋๋ ์ด๋ณ๋ ๋ฐ์ดํฐ์ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ์ถ์ ํ์ฌ ๊ทธ๋ฆฝ๋๋ค. ์ฌ๊ธฐ์ ํํ๋(bandwidth)์ ์กฐ์ ํ๋ ค๋ฉด `bw_adjust` ์ต์
์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. ์ด ์ต์
์ ํฌ๊ธฐ์ ๋ฐ๋ผ ํ๋ฅ ๋ฐ๋ํจ์์ ๋ถ๋๋ฌ์(smoothness) ์ ๋๊ฐ ๋ฌ๋ผ์ง๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.kdeplot(data=df_penguins, x='flipper_length_mm') # ์ฝ๋1sns.kdeplot(data=df_penguins, x='flipper_length_mm', bw_adjust=.25) ์ฝ๋ 2
```
`displot()` ํจ์์ `kind='kde'`์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
```
pythonsns.displot(penguins, x='flipper_length_mm', kind='kde') # ์ฝ๋1sns.displot(penguins, x='flipper_length_mm', kind='kde', bw_adjust=.25) # ์ฝ๋2
```




kdeplot ํจ์๋ก ๊ทธ๋ฆฐ ํ๋ฅ ๋ฐ๋ํจ์
๋ง์ผ `displot()` ํจ์์ `kde=True` ์ต์
์ ์ง์ ํ๋ฉด ํ์คํ ๊ทธ๋จ๊ณผ ์ปค๋๋ฐ๋์ถ์ ํจ์๋ฅผ ๋์์ ๊ทธ๋ฆด ์ ์์ต๋๋ค.
#### ๋ฒ์ ์ ํ: cut
์ฐ์์ ๋ณ์๊ฐ ๋ฌดํํ ์ปค์ง์ง ์๋ ๊ฒฝ์ฐ์๋ `cut`์ด๋ผ๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํด ์ ๋์ ๋ฐ์ดํฐ ํฌ์ธํธ ๋ฒ์๋ฅผ ์ ํํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
```
pythonsns.kdeplot(df_tips, x='total_bill', kind='kde') # ์ฝ๋1sns.kdeplot(df_tips, x='total_bill', kind='kde', cut=0) # ์ฝ๋2
```




์ ๋์ ๋ฐ์ดํฐ ํฌ์ธํธ ๋ฒ์๋ฅผ ์ ํํ ํ๋ฅ ๋ฐ๋ํจ์
#### ์์: shade
KDE ๋ฐ๋ ๊ณก์ ์๋์ ์์ ์ฑ์ฐ๊ณ ์ถ๋ค๋ฉด `shade=True` ์ต์
์ ์ฃผ๋ฉด ๋ฉ๋๋ค.
### 5\) ๊ฒฝํ์ ๋์ ๋ถํฌํจ์: edcfplot()
๊ฒฝํ์ ๋์ ๋ถํฌํจ์(empirical cumulative distribution function, ECDF)๋ n๊ฐ์ ๋ฐ์ดํฐ ํฌ์ธํธ ๊ฐ๊ฐ์์ 1/n ์ฉ ์ ํํ๋ ๊ณ๋จ ํจ์์
๋๋ค. ๊ฐ๋จํ CDF๋ผ๊ณ ๋ ํฉ๋๋ค. ECDF ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด `ecdfplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.ecdfplot(df_penguins, x='flipper_length_mm')
```




ecdfplot() ํจ์๋ก ๊ทธ๋ฆฐ ๊ฒฝํ์ ๋์ ๋ถํฌํจ์
`displot()` ํจ์์ `kind='ecdf'` ์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
```
pythonsns.displot(df_penguins, x='flipper_length_mm', kind='ecdf')
```
์ง๊ธ๊น์ง ํ์ด์ฌ Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก 1์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด์์ต๋๋ค. ๋ค์ ์๊ฐ์๋ [ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ์ฌํํธ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-advanced/)์์ ๋ค์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ๋ชจ๋ ์๊ณ ๋ง์ผ์
จ์ต๋๋ค.
## ์ฐธ๊ณ ๋ฌธํ
- \[1\] ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค ์ค์ฟจ, ๏ฝขํ์ด์ฌํธ 5์ฅ ๋ฐ์ดํฐ ์๊ฐํ: Seaborn์ ์ฌ์ฉํ ๋ฐ์ดํฐ ๋ถํฌ ์๊ฐํ๏ฝฃ, ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค ์ค์ฟจ, "<https://datascienceschool.net/>"
- \[2\] ์๋ฌดํผ์๋ผ๋ฒจ, ๏ฝข์ ๊ทํ(Normalization) ์ฝ๊ฒ ์ดํดํ๊ธฐ๏ฝฃ, ์๋ฌดํผ์๋ผ๋ฒจ, "<https://hleecaster.com/ml-normalization-concept/>"
- \[3\] ์๋ฌดํผ์๋ผ๋ฒจ, ๏ฝข\[seaborn\] ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ๏ฝฃ, ์๋ฌดํผ์๋ผ๋ฒจ, "<https://hleecaster.com/python-seaborn-violinplot/>"
- \[4\] Codeacademy Team, ๏ฝขSeaborn Styling, Part 1: Figure Style and Scale๏ฝฃ, Codecademy, "<https://www.codecademy.com/article/seaborn-design-i>"
- \[5\] Codeacademy Team, ๏ฝขSeaborn Styling, Part 2: Color๏ฝฃ, Codecademy, "<https://www.codecademy.com/article/seaborn-design-ii>"
- \[6\] Mahbubul Alam, ๏ฝขSeaborn can do the job, then why Matplotlib?๏ฝฃ, Towards Data Science, "<https://towardsdatascience.com/seaborn-can-do-the-job-then-why-matplotlib-dac8d2d24a5f>"
- \[7\] Seaborn, ๏ฝขseaborn: statistical data visualization๏ฝฃ, Seaborn, "<https://seaborn.pydata.org/index.html>"
- \[8\] StackOverflow, ๏ฝขHow to set a different color to the largest bar in a seaborn barplot๏ฝฃ, StackOverflow, "<https://stackoverflow.com/questions/31074758/how-to-set-a-different-color-to-the-largest-bar-in-a-seaborn-barplot>"
๋ค์ ๊ธ
[ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ์ฌํํธ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-advanced/)
[ํ์ผ๋ก ๋์๊ฐ๊ธฐ](https://www.snugarchive.com/)
0 Comments
#### ์๊ฐ


์น ๊ฐ๋ฐ, ๋ฐ์ดํฐ ๋ถ์
- - [์ปดํจํฐ ๊ณผํ2](https://www.snugarchive.com/blog/category/computer-science/)
- [๊ธฐ์ด2](https://www.snugarchive.com/blog/category/computer-science/basic/)
- [๋ฐ์ดํฐ ๊ณผํ9](https://www.snugarchive.com/blog/category/data-science/)
- [๋ฐ์ดํฐ ์์ง1](https://www.snugarchive.com/blog/category/data-science/data-collection/)
- [๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ5](https://www.snugarchive.com/blog/category/data-science/data-preprocessing/)
- [๋ฐ์ดํฐ ์๊ฐํ3](https://www.snugarchive.com/blog/category/data-science/visualization/)
- [ํ๊ฒฝ ์ค์ 12](https://www.snugarchive.com/blog/category/environment-setup/)
- [๊ฐ๋ฐ12](https://www.snugarchive.com/blog/category/environment-setup/development/)
- [์ํ1](https://www.snugarchive.com/blog/category/mathematics/)
- [ํต๊ณ1](https://www.snugarchive.com/blog/category/mathematics/statistics/)
- [์์ฐ์ด7](https://www.snugarchive.com/blog/category/natural-language/)
- [์์ด5](https://www.snugarchive.com/blog/category/natural-language/english/)
- [ํ๊ตญ์ด2](https://www.snugarchive.com/blog/category/natural-language/korean/)
- [ํ๋ก๊ทธ๋๋ฐ ์ธ์ด4](https://www.snugarchive.com/blog/category/programming-language/)
- [C3](https://www.snugarchive.com/blog/category/programming-language/c/)
- [Kotlin1](https://www.snugarchive.com/blog/category/programming-language/kotlin/)
- [์น ๊ฐ๋ฐ6](https://www.snugarchive.com/blog/category/web-development/)
- [HTML2](https://www.snugarchive.com/blog/category/web-development/html/)
- [React4](https://www.snugarchive.com/blog/category/web-development/react/)
- - [์น ์ฑ2](https://www.snugarchive.com/blog/category/web-app/)
- [๋์๋ณด๋2](https://www.snugarchive.com/blog/category/web-app/dashboard/)
#### ํ๊ทธ
๋ฆฌ์คํธ ํผ์ณ๋ณด๊ธฐ
[C ๊ธฐ์ด(3)](https://www.snugarchive.com/tag/c-%EA%B8%B0%EC%B4%88/)
[ChartJS(1)](https://www.snugarchive.com/tag/chartjs/)
[ECharts(1)](https://www.snugarchive.com/tag/echarts/)
[Gatsby(1)](https://www.snugarchive.com/tag/gatsby/)
[HTML ๊ธฐ์ด(2)](https://www.snugarchive.com/tag/html-%EA%B8%B0%EC%B4%88/)
[HTML ์์(1)](https://www.snugarchive.com/tag/html-%EC%9A%94%EC%86%8C/)
[HTML ํ๊ทธ(1)](https://www.snugarchive.com/tag/html-%ED%83%9C%EA%B7%B8/)
[IDE(3)](https://www.snugarchive.com/tag/ide/)
[JDK(2)](https://www.snugarchive.com/tag/jdk/)
[Kotlin ๊ธฐ์ด(1)](https://www.snugarchive.com/tag/kotlin-%EA%B8%B0%EC%B4%88/)
[MySQL(1)](https://www.snugarchive.com/tag/mysql/)
[Node(2)](https://www.snugarchive.com/tag/node/)
[pandas ์ฌ์ฉ๋ฒ(5)](https://www.snugarchive.com/tag/pandas-%EC%82%AC%EC%9A%A9%EB%B2%95/)
[Python Pandas ๊ธฐ์ด(4)](https://www.snugarchive.com/tag/python-pandas-%EA%B8%B0%EC%B4%88/)
[Python Seaborn(2)](https://www.snugarchive.com/tag/python-seaborn/)
[Python ์ค์ต(1)](https://www.snugarchive.com/tag/python-%EC%8B%A4%EC%8A%B5/)
[React(1)](https://www.snugarchive.com/tag/react/)
[React ๊ธฐ์ด(4)](https://www.snugarchive.com/tag/react-%EA%B8%B0%EC%B4%88/)
[React ์์(2)](https://www.snugarchive.com/tag/react-%EC%8B%9C%EC%9E%91/)
[React ์ ์(1)](https://www.snugarchive.com/tag/react-%EC%A0%95%EC%9D%98/)
[React ํ๊ฒฝ ์ค์ (1)](https://www.snugarchive.com/tag/react-%ED%99%98%EA%B2%BD-%EC%84%A4%EC%A0%95/)
[Spring(5)](https://www.snugarchive.com/tag/spring/)
[Visual Studio(1)](https://www.snugarchive.com/tag/visual-studio/)
[๊ฐ์ ํํ ์์ด(4)](https://www.snugarchive.com/tag/%EA%B0%90%EC%A0%95-%ED%91%9C%ED%98%84-%EC%98%81%EC%96%B4/)
[๋งํฌ์
์ธ์ด(1)](https://www.snugarchive.com/tag/%EB%A7%88%ED%81%AC%EC%97%85-%EC%96%B8%EC%96%B4/)
[์ด๊ฐ(2)](https://www.snugarchive.com/tag/%EC%96%B4%EA%B0%90/)
[์์ด ๋ฌธ๋ฒ ์ฉ์ด(1)](https://www.snugarchive.com/tag/%EC%98%81%EC%96%B4-%EB%AC%B8%EB%B2%95-%EC%9A%A9%EC%96%B4/)
[์์ด ์ฝ์ด(1)](https://www.snugarchive.com/tag/%EC%98%81%EC%96%B4-%EC%95%BD%EC%96%B4/)
[์์ด ์ค์๋ง(1)](https://www.snugarchive.com/tag/%EC%98%81%EC%96%B4-%EC%A4%84%EC%9E%84%EB%A7%90/)
[์ฃผํผํฐ ๋
ธํธ๋ถ(1)](https://www.snugarchive.com/tag/%EC%A3%BC%ED%94%BC%ED%84%B0-%EB%85%B8%ED%8A%B8%EB%B6%81/)
[ํฌ๋กค๋ง ์คํฌ๋ํ ์ฐจ์ด(1)](https://www.snugarchive.com/tag/%ED%81%AC%EB%A1%A4%EB%A7%81-%EC%8A%A4%ED%81%AC%EB%9E%98%ED%95%91-%EC%B0%A8%EC%9D%B4/)
[ํ
์คํธ ์๋ํฐ ์ถ์ฒ(1)](https://www.snugarchive.com/tag/%ED%85%8D%EC%8A%A4%ED%8A%B8-%EC%97%90%EB%94%94%ED%84%B0-%EC%B6%94%EC%B2%9C/)
[ํต๊ณ ๊ธฐ์ด(1)](https://www.snugarchive.com/tag/%ED%86%B5%EA%B3%84-%EA%B8%B0%EC%B4%88/)
[ํตํฉ ๊ฐ๋ฐ ํ๊ฒฝ(IDE) ์ถ์ฒ(1)](https://www.snugarchive.com/tag/%ED%86%B5%ED%95%A9-%EA%B0%9C%EB%B0%9C-%ED%99%98%EA%B2%BD\(ide\)-%EC%B6%94%EC%B2%9C/)
[ํ์ด์ฌ ๊ธฐ์ด(1)](https://www.snugarchive.com/tag/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EA%B8%B0%EC%B4%88/)
[ํ๋ก๊ทธ๋๋ฐ ๊ธฐ์ด(2)](https://www.snugarchive.com/tag/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-%EA%B8%B0%EC%B4%88/)
[ํ์ดํผํ
์คํธ(1)](https://www.snugarchive.com/tag/%ED%95%98%EC%9D%B4%ED%8D%BC%ED%85%8D%EC%8A%A4%ED%8A%B8/)
[ํ๊ตญ์ด๋ฅ๋ ฅ์ํ(2)](https://www.snugarchive.com/tag/%ED%95%9C%EA%B5%AD%EC%96%B4%EB%8A%A5%EB%A0%A5%EC%8B%9C%ED%97%98/)
[ํท๊ฐ๋ฆฌ๋ ํ๊ตญ์ด ๋จ์ด(2)](https://www.snugarchive.com/tag/%ED%97%B7%EA%B0%88%EB%A6%AC%EB%8A%94-%ED%95%9C%EA%B5%AD%EC%96%B4-%EB%8B%A8%EC%96%B4/)
#### ์ต๊ทผ๊ธ
[   ํ๊ฒฝ ์ค์ 2023-10-26 SQL Developer ๋ค์ด๋ก๋ ๋ฐ ์ฌ์ฉ๋ฒ](https://www.snugarchive.com/blog/sql-developer-setup/)
[   ํ๊ฒฝ ์ค์ 2023-08-31 DBMS ์ค์นํ๊ธฐ(์ค๋ผํด 21c XE, MySQL)](https://www.snugarchive.com/blog/dbms-installation/)
[   ํ๊ฒฝ ์ค์ 2023-08-21 STS 4/STS 3 ์ค์น ๋ฐ ํ๊ฒฝ ์ค์ ํ๊ธฐ](https://www.snugarchive.com/blog/sts-setup/)
[   ํ๊ฒฝ ์ค์ 2023-08-21 ์ํ์น ํฐ์บฃ(Apache Tomcat) ๋ค์ด๋ก๋ ๋ฐ ํ๊ฒฝ์ค์ ํ๊ธฐ](https://www.snugarchive.com/blog/apache-tomcat-setup/)
[   ๋ฐ์ดํฐ ๊ณผํ 2023-08-04 ECharts ์ฌ์ฉ๋ฒ๊ณผ ์์ ](https://www.snugarchive.com/blog/echarts-tutorial/)
#### ์ธ๊ธฐ๊ธ
[   ๋ฐ์ดํฐ ๊ณผํ 2022-04-19 ํ์ด์ฌ ์น ํฌ๋กค๋ง? ์น ์คํฌ๋ํ ๊ฐ๋
๊ณผ ์ค์ต ์์ ](https://www.snugarchive.com/blog/python-web-scraping/)
[   ํ๊ฒฝ ์ค์ 2022-04-14 ํตํฉ ๊ฐ๋ฐ ํ๊ฒฝ(IDE), ํ
์คํธ ์๋ํฐ ์ธ๊ธฐ ์์ (2022๋
)](https://www.snugarchive.com/blog/best-ide-text-editors/)
[   ํ๊ฒฝ ์ค์ 2023-08-21 ์ํ์น ํฐ์บฃ(Apache Tomcat) ๋ค์ด๋ก๋ ๋ฐ ํ๊ฒฝ์ค์ ํ๊ธฐ](https://www.snugarchive.com/blog/apache-tomcat-setup/)
[   ํ๊ฒฝ ์ค์ 2022-04-16 ์ฃผํผํฐ ๋
ธํธ๋ถ ํ๊ฒฝ ์ค์ ํ๊ธฐ](https://www.snugarchive.com/blog/jupyter-notebook-setup/)
[   ๋ฐ์ดํฐ ๊ณผํ 2022-06-05 ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ๊ธฐ์ดํธ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/)
#### ์ต๊ทผํ๋ก์ ํธ
[   ์น ์ฑ 2023-06-20 ์ง์ง ๋ชจ๋ํฐ๋ง ์ํฉํ ํ๋ก์ ํธ](https://www.snugarchive.com/blog/earthquake-dashboard/)
[   ์น ์ฑ 2021-12-01 ์ฝ๋ก๋19(COVID-19) ์ํฉํ ํ๋ก์ ํธ](https://www.snugarchive.com/blog/covid19-dashboard/)
#### ๋ชฉ์ฐจ
1. [์ค๋นํ๊ธฐ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%A4%80%EB%B9%84%ED%95%98%EA%B8%B0)
- [์๋ด ์ฌํญ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%95%88%EB%82%B4-%EC%82%AC%ED%95%AD)
- [์ค์นํ๊ธฐ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0)
- [๊ธฐ๋ณธ ํ๊ฒฝ ์ค์ ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EA%B8%B0%EB%B3%B8-%ED%99%98%EA%B2%BD-%EC%84%A4%EC%A0%95)
- [์ ์ฒด ์คํ์ผ๋ง](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%A0%84%EC%B2%B4-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
- [๊ทธ๋ํ๋ณ ์คํ์ผ๋ง](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EA%B7%B8%EB%9E%98%ED%94%84%EB%B3%84-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
- [๋ฐ์ดํฐ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EB%8D%B0%EC%9D%B4%ED%84%B0)
2. [1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ๋ฒ์ฃผํ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#1%EC%B0%A8%EC%9B%90-%EB%8D%B0%EC%9D%B4%ED%84%B0-%EC%8B%9C%EA%B0%81%ED%99%94-%EB%B2%94%EC%A3%BC%ED%98%95)
- [1\) ๋น๋ ๋ง๋๊ทธ๋ํ: countplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#1-%EB%B9%88%EB%8F%84-%EB%A7%89%EB%8C%80%EA%B7%B8%EB%9E%98%ED%94%84-countplot)
- [2\) ์์ ๊ทธ๋ฆผ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#2-%EC%83%81%EC%9E%90-%EA%B7%B8%EB%A6%BC)
3. [1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ์์นํ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#1%EC%B0%A8%EC%9B%90-%EB%8D%B0%EC%9D%B4%ED%84%B0-%EC%8B%9C%EA%B0%81%ED%99%94-%EC%88%98%EC%B9%98%ED%98%95)
- [1\) ์ ๊ทธ๋ํ: stripplot(), swarmplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#1-%EC%A0%90%EA%B7%B8%EB%9E%98%ED%94%84-stripplot-swarmplot)
- [2\) ์ ๋ถ๊ทธ๋ํ: rugplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#2-%EC%84%A0%EB%B6%84%EA%B7%B8%EB%9E%98%ED%94%84-rugplot)
- [3\) ํ์คํ ๊ทธ๋จ: histplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#3-%ED%9E%88%EC%8A%A4%ED%86%A0%EA%B7%B8%EB%9E%A8-histplot)
- [4\) ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์: kdeplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#4-%EC%BB%A4%EB%84%90%EB%B0%80%EB%8F%84%EC%B6%94%EC%A0%95%EC%9C%BC%EB%A1%9C-%EA%B5%AC%ED%95%9C-%ED%99%95%EB%A5%A0%EB%B0%80%EB%8F%84%ED%95%A8%EC%88%98-kdeplot)
- [5\) ๊ฒฝํ์ ๋์ ๋ถํฌํจ์: edcfplot()](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#5-%EA%B2%BD%ED%97%98%EC%A0%81-%EB%88%84%EC%A0%81%EB%B6%84%ED%8F%AC%ED%95%A8%EC%88%98-edcfplot)
4. [์ฐธ๊ณ ๋ฌธํ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-basic/#%EC%B0%B8%EA%B3%A0-%EB%AC%B8%ED%97%8C)
ยฉ2026 Snug Archive. All rights reserved.
Email: snugarchive@gmail.com |
| Readable Markdown | ## 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ
Last Updated 2024-09-10 Published 2022-06-05[Python Seaborn](https://www.snugarchive.com/tag/python-seaborn/)8๋ถ
๋ชฉ์ฐจ

Seaborn์ผ๋ก ์ผ๋ณ๋ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํ ํด๋ณด์
ํ์ด์ฌ(Python)์๋ [Matplotlib(๋งทํ๋กฏ๋ฆฝ)](https://matplotlib.org/), Plotly(ํ๋กํ๋ฆฌ), GGplot(์ง์งํ๋กฏ) ๋ฑ ๋ค์ํ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์์ต๋๋ค. Matplotlib์ ์ ์ธ๊ณ์ ๋ฐ์ดํฐ ๊ณผํ์๋ค์ด ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. Plotly๋ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ plotly.js๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค์ด์ ธ, ๊ทธ๋ํ์ ํน์ ๋ถ๋ถ์ ํ๋/์ถ์ํ๊ฑฐ๋ ์ ์ฅํ๋ ๋ฑ ์น ์์์ ์ฌ์ฉ์์ ์ํธ์์ฉํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. GGplot์ R์ ggplot2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ฐํ์ผ๋ก ๊ฐ๋ฐ๋์ด, ๊ธฐ์กด์ R ์ฌ์ฉ์๋ค์ด ์ฌ์ฉํ๊ธฐ ํธ๋ฆฌํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
๊ทธ๋ ๋ค๋ฉด Seaborn์ด๋ ๋ฌด์์ผ๊น์? ๋, ๋ง์ ์๊ฐํ ๋๊ตฌ ์ค์์ Seaborn์ ์ฌ์ฉํ๋ฉด ์ข์ ์ด์ ๋ ๋ฌด์์ผ๊น์? Seaborn์ Matplotlib์ ์ฝ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด ๊ฐ๋ฐ๋ ๊ณ ์์ค(high-level) ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. Seaborn์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ ๊ฐ๊ฒฐํจ์
๋๋ค. Seaborn์ ์ด์ฉํ๋ฉด ๋น๊ต์ ์งง์ ์ฝ๋๋ก๋ ํต๊ณํ์ ์ฃผ์ ๊ทธ๋ํ๋ฅผ ๋น ๋ฅด๊ณ ํธ๋ฆฌํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. ๊ทธ๋ํ๋ฅผ ์ธ๋ถ ์ค์ ์์ด ๊ฐ๋จํ๊ณ ๋น ๋ฅด๊ฒ ๊ทธ๋ฆฌ๊ณ ์ถ๋ค๋ฉด Matplotlib๋ณด๋ค Seaborn์ ์ฌ์ฉํ๋ฉด ์ข์ต๋๋ค.
Seaborn์ ์ฌ์ฉ๋ฒ์ ๊ธฐ์ดํธ๊ณผ ์ฌํํธ์ผ๋ก ๋๋ฉ๋๋ค. ๊ธฐ์ดํธ์์๋ Seaborn์ ์ค์นํ๊ณ ์ค์ต์ ์ํ ๊ธฐ๋ณธ์ ์ธ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ๊ณผ ๋ณ์๊ฐ 1๊ฐ์ธ 1์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ๋ค๋ฃน๋๋ค. [ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ์ฌํํธ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-advanced/)์์๋ ๋ณ๋์ด 2๊ฐ ์ด์์ธ ๋ค์ฐจ์ ๊ทธ๋ํ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ๊ธฐ๋ณธํธ์์ ๋ค๋ฃฐ ์ ์ฒด ๊ทธ๋ํ์ ๊ฐ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.

Seaborn 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ ๋ก๋๋งต
๊ทธ๋ผ Seaborn์ผ๋ก 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ๋ฅผ ํ๊ธฐ ์ ์ ์ค๋นํ ์ฌํญ๋ถํฐ ์์๋ณด๊ฒ ์ต๋๋ค.
## ์ค๋นํ๊ธฐ
### ์๋ด ์ฌํญ
Seaborn์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๊ธฐ ์ํด์๋ ๋ช ๊ฐ์ง ์ค๋น๊ฐ ํ์ํฉ๋๋ค. ์ฒซ์งธ, ์ค์ต ํ๊ฒฝ์
๋๋ค. ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค(Data Science)๋ฅผ ์ํ ํตํฉ๊ฐ๋ฐํ๊ฒฝ(IDE)์๋ ๋ํ์ ์ผ๋ก ์คํ์ด๋(Spyder), ์ํฐ(Atom), ํ์ด์ฐธ(PyCharm) ๋ฑ์ด ์์ต๋๋ค. ์ด ๊ธ์์๋ ์ฃผํผํฐ ๋
ธํธ๋ถ(Jupyter Notebook)์ ์ฌ์ฉํฉ๋๋ค. ์ฃผํผํฐ ๋
ธํธ๋ถ์ ์์ธํ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ์ [์ฃผํผํฐ ๋
ธํธ๋ถ ํ๊ฒฝ ์ค์ ํ๊ธฐ](https://www.snugarchive.com/blog/jupyter-notebook-setup/)๋ฅผ ์ฐธ์กฐํด ์ฃผ์ธ์.
๋์งธ, ํต๊ณ ์ฉ์ด์ ๋ํ ๊ฐ๋จํ ์ดํด๊ฐ ํ์ํฉ๋๋ค. ๊ฐ ์ฉ์ด๋ ๊ฐ๋ตํ ์ค๋ช
ํ ์์ ์ด๋, ๊ฐ๋
์ ๋ํ ์์ธํ ์ค๋ช
์ด๋ ์์์ ๋ค๋ฃจ์ง ์์ต๋๋ค. ํต๊ณ ์ฉ์ด๋ฅผ ์ฐธ์กฐํ๋ฉด์ ๊ธ์ ์ฝ๊ณ ์ถ์ ๋ถ๋ค์ [ํ๋ฅ ๊ณผ ํต๊ณ ๊ธฐ์ด ์ฉ์ด](https://www.snugarchive.com/blog/glossary-statistical-terms/)๋ฅผ ํจ๊ป ์ฝ์ผ์๊ธฐ๋ฅผ ๊ถํด๋๋ฆฝ๋๋ค.
์
์งธ, Seaborn ํจ์์ ์ข
๋ฅ์
๋๋ค. Seaborn์ ์๊ฐํ ํจ์๋ ๊ทธ๋ํ ์์ค(figure-level)์ ํจ์์ ์ถ ์์ค(axes-level)์ ํจ์๋ก ๋๋ฉ๋๋ค. ๊ทธ๋ํ ์์ค์ ํจ์๋ ์์ ํจ์๋ก ๊ทธ๋ํ์ ์ข
๋ฅ๋ฅผ ์ง์ ํ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํด ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํฉ๋๋ค. ๋ฐ๋ฉด ์ถ ์์ค ํจ์๋ ๊ฐ ๊ทธ๋ํ์ ์ข
๋ฅ์ ํนํ๋ ํจ์์
๋๋ค. ์ถ ์์ค ํจ์๋ 1๊ฐ์ง ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฐ ๋ง์ถคํ๋์ด ์์ต๋๋ค.
์ด ๋ ์ข
๋ฅ์ ํจ์๋ฅผ ๊ตฌ๋ถํ๋ ๊ธฐ์ค์ `Grid`์ ์์ฑ ์ฌ๋ถ์
๋๋ค. `displot()`, `catplot()`, `relplot()` ํจ์๋ ๋ชจ๋ ๊ทธ๋ํ ์์ค ํจ์์ด๋ฉฐ `seaborn.axisgrid.FacetGrid`๋ฅผ ๋ง๋ญ๋๋ค. ๋ฐ๋ฉด, `countplot()`, `hisplot()`, `striplot()` ๋ฑ๊ณผ ๊ฐ์ ํจ์๋ ์ถ ์์ค ํจ์์ด๋ฉฐ ๊ฒฐ๊ณผ๋ก `AxesSubplot`์ ๋ง๋ญ๋๋ค. `FacetGrid`๋ ์ฌ๋ฌ ๊ทธ๋ํ๋ฅผ ํฌํจํ๋ ์์ ๊ทธ๋ํ๋ก, `FacetGrid`์์ ํน์ ํ์ `AxesSubplot` ๊ทธ๋ํ๋ง ์ถ์ถํด ์ํ๋ ์กฐ๊ฑด์ ์ ์ฉํ ์ ์์ต๋๋ค.
์ฌ์ฉํ๋ ์ต์
์ด ์๋ก ๋ค๋ฅธ ๊ฒฝ์ฐ๋ ์์ง๋ง ๋ณดํต ๊ทธ๋ํ ์์ค ํจ์์ ์ถ ์์ค ํจ์์ ์ต์
์ ์๋ก ํธํ๋ฉ๋๋ค. ๋ค๋ง, Matplotlib ๊ณผ์ ํธํ์ฑ์ด๋ ํ ๊ทธ๋ํ ์์ ๋ค๋ฅธ ๊ทธ๋ํ๋ฅผ ๊ฒน์ณ ๊ทธ๋ฆด ์ ์๋ค๋ ์ ์์๋ ์ถ ์์ค์ ํจ์๊ฐ ๊ทธ๋ํ ์์ค ํจ์๋ณด๋ค ์กฐ๊ธ ๋ ์ ์ฐํฉ๋๋ค. ์ฌ๊ธฐ์๋ ๊ทธ๋ํ ์์ค์ ํจ์๋ฅผ ์ค์ฌ์ผ๋ก ๊ทธ๋ํ๋ฅผ ์ดํด๋ณด๋, ๊ทธ๋ํ ์์ค์ผ๋ก ๊ทธ๋ฆด ์ ์๋ ๊ทธ๋ํ๋ ์ถ ์์ค ํจ์๋ก ๊ทธ๋ฆฌ๊ฒ ์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ์ง๊ธ๋ถํฐ Seaborn์ ์ค์นํด ๋ณด๊ฒ ์ต๋๋ค.
### ์ค์นํ๊ธฐ
#### 1\) ํ์ด์ฌ ๋ฐ pip ์ค์น ์ฌ๋ถ ํ์ธ
Seaborn์ ์ฌ์ฉํ๋ ค๋ฉด ํ์ด์ฌ๊ณผ ํ์ด์ฌ์ ํจํค์ง ๊ด๋ฆฌ ๋งค๋์ ์ธ `pip`์ด ํ์ํฉ๋๋ค. ์ฌ์ฉํ๊ณ ์๋ ์์คํ
์ ํ์ด์ฌ๊ณผ `pip`์ด ์ค์น๋์ด ์๋์ง ํ์ธํฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonpython -Vpip -v
```
ํ์ด์ฌ๊ณผ `pip`์ด ์ ์ค์น๋์ด ์๋ค๋ฉด Seaborn์ ์ค์นํ ์ค๋น๊ฐ ๋ ๊ฒ์
๋๋ค. ๊ทธ๋ฌ๋ฉด ์ด์ Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๊ฒ ์ต๋๋ค.
#### 2\) ํจํค์ง ์ค์น
ํฐ๋ฏธ๋์ `pip install`์ด๋ผ๋ ๋ช
๋ น์ด ๋ค์ ์ค์นํ๋ ค๋ ํจํค์ง์ ์ด๋ฆ์ธ `seaborn`์ ์
๋ ฅํฉ๋๋ค.
```
pythonpip install seaborn
```
ํ์ด์ฌ/R ๋ฐฐํฌํ์ธ ์๋์ฝ๋ค(Anaconda)๋ก ์์
ํ์๋ ๋ถ๋ค์ ์๋์ ๊ฐ์ด `pip` ๋ช
๋ น์ด ๋์ `conda` ๋ช
๋ น์ด๋ฅผ ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค.
```
pythonconda install seaborn
```
#### 3\) ์ค์น ํ์ธ
์ค์น ๋ช
๋ น์ด๋ฅผ ์คํํ๋ค๋ฉด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์ ๋ก ์ ์ค์น๋์๋์ง ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค. ํจํค์ง์ ์ค์น ์ฌ๋ถ๋ฅผ ์๊ณ ์ถ๋ค๋ฉด ์ค์น๋ ํจํค์ง์ ๋ฒ์ ์ ๋ณด๋ฅผ ํ์ธํ๋ฉด ๋ฉ๋๋ค. ๋ค์ ์ฝ๋๋ฅผ ์
๋ ฅํด์ ์ค์น๋ Seaborn์ ๋ฒ์ ์ ๋ณด๊ฐ ๋ณด์ด๋ฉด Seaborn์ด ์ ์ค์น๋ ๊ฒ์
๋๋ค.
```
pythonimport seaborn as snssns.__version__
```
๊ทธ๋ํ๋ฅผ ์ถ๋ ฅํด์ ์ค์น ์ฌ๋ถ๋ฅผ ํ์ธํ ์๋ ์์ต๋๋ค. ์ฝ๋๋ ์๋์ ๊ฐ์ต๋๋ค.
```
pythonimport seaborn as snsdf = sns.load_dataset('penguins')sns.pairplot(df, hue='species')
```
Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น๋ฅผ ์๋ฃํ๋ค๋ฉด ๋ค์์ ๊ธฐ๋ณธ์ ์ธ ํ๊ฒฝ ์ค์ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
### ๊ธฐ๋ณธ ํ๊ฒฝ ์ค์
๊ธฐ๋ณธ ํ๊ฒฝ ์ค์ ์ ๊ทธ๋ํ ์ ์ญ์ ์ ์ฉ๋๋ ์คํ์ผ๋ง(styling)์
๋๋ค. ์ฝ๋๋ณ ํ๊ฒฝ ์ค์ ์ ๋ํ ์์ธํ ์ค๋ช
์ ํ์ด์ฌ Matplotlib ์ฌ์ฉ๋ฒ(์์ )์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom matplotlib import rcParamsimport seaborn as snsimport warningsdef setting_styles_basic():rcParams['font.family'] = 'Malgun Gothic'rcParams['axes.unicode_minus'] = Falsesetting_styles_basic()warnings.filterwarnings('ignore')
```
Matplotlib์ ์ด์ฉํ์ง ์๊ณ Seaborn์ผ๋ก ํ๊ฒฝ ์ค์ ์ ํ ์๋ ์์ต๋๋ค.
### ์ ์ฒด ์คํ์ผ๋ง
Seaborn์์ ๋ชจ๋ ์คํ์ผ๋ง์ ํ ๋ฒ์ ์ค์ ํ๋ ค๋ฉด `set_theme()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
> - set\_theme: ๊ทธ๋ํ ์ ๋ฐ ๋ฐ ๋งค์ฒด๋ณ ์ค์ผ์ผ(scale), ์์ ํ๋ ํธ ์ง์
๋ค์ ํจ์๋ `set_theme()`์ ์ผ์ ์ญํ ๋ถ๋ดํฉ๋๋ค.
> - set\_style: ๊ทธ๋ํ ์ ๋ฐ ์คํ์ผ ์ง์
> - set\_context: ๋งค์ฒด๋ณ ์ค์ผ์ผ ์ง์
> - set\_palette: ์์ ํ๋ ํธ ์ง์
#### set\_theme
`set_theme()` ํจ์๋ ๊ทธ๋ํ ์ ๋ฐ์ ์ ์ฉ๋๋ ํ
๋ง(theme)๋ฅผ ์ง์ ํ๋ ํจ์์
๋๋ค. `set_theme()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๊ทธ๋ํ ์ ์ญ์ ์คํ์ผ๋ง์ ์ง์ ํ๋ `set_style()` ํจ์์ ์ฌ์ฉํ ๋งค์ฒด์ ์ ํฉํ๋๋ก ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์กฐ์ ํ๋ `set_context()` ํจ์๋ก ํ๋ ์ผ์ ํ ๋ฒ์ ํ ์ ์์ต๋๋ค. ์ฌ์ฉ ์์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythoncustom_params = {"axes.spines.right": False, "axes.spines.top": False}sns.set_theme(context='notebook',style='darkgrid',palette='deep',font='Malgun Gothic',font_scale=1,rc=custom_params)
```
##### context
`context` ํ๋ผ๋ฏธํฐ๋ ์ฌ์ฉํ๋ ๋งค์ฒด์ ์ ํฉํ ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์กฐ์ ํ๋ ์ต์
์
๋๋ค. ์ด ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๊ฐ ๋งค์ฒด์ ์ ํฉํ๊ฒ ๋ผ๋ฒจ๊ณผ ๊ทธ๋ํ์ ํฌ๊ธฐ๋ฅผ ๋ง์ถค ์ค์ ํ ์ ์์ต๋๋ค. ์ ํํ ์ ์๋ ์ต์
์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
> - notebook: ๊ธฐ๋ณธ ์ค์
> - paper: ๋
ผ๋ฌธ, ๋ณด๊ณ ์
> - talk: ํ๋ฆฌ์ ํ
์ด์
> - poster: ํฌ์คํฐ
##### style
`style` ํ๋ผ๋ฏธํฐ๋ Seaborn์ ๊ธฐ๋ณธ ๋ด์ฅ ํ
๋ง(built-in themes)๋ฅผ ์ง์ ํ๋ ์ต์
์
๋๋ค. ๋ด์ฅ ํ
๋ง์๋ ์ด 5๊ฐ์ง ํ
๋ง๊ฐ ์์ต๋๋ค.
> - darkgrid: ํ์ ๋ฐฐ๊ฒฝ์ + ๊ทธ๋ฆฌ๋
> - whitegrid: ํฐ์ ๋ฐฐ๊ฒฝ์ + ๊ทธ๋ฆฌ๋
> - dark: ํ์ ๋ฐฐ๊ฒฝ์
> - white: ํฐ์ ๋ฐฐ๊ฒฝ์
> - ticks: ํฐ์ ๋ฐฐ๊ฒฝ์ + ๋๊ธ
##### palette
`palette` ํ๋ผ๋ฏธํฐ๋ ๊ทธ๋ํ์ ์์ ์ง์ ํ๋ ์ต์
์
๋๋ค. ์ฌ์ฉํ ์ ์๋ ๊ธฐ๋ณธ ์์ ํ๋ ํธ๋ ์ด 6๊ฐ์ง(`deep`, `muted`, `pastel`, `bright`, `dark`, `colorblind`)์
๋๋ค. ํน์ ํ๋ ํธ๋ฅผ ์ ํํ๋ ค๋ฉด `color_palette()` ํจ์๋ฅผ, ์ ํํ ํ๋ ํธ์ ์์์ ํ์ธํ๊ณ ์ถ๋ค๋ฉด `palplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonpalette = sns.color_palette('deep')sns.palplot(palette)
```
##### font, font\_scale
`font`์ `font_scale`์ ๊ฐ๊ฐ ๊ธ๊ผด์ ์ข
๋ฅ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๋ ํ๋ผ๋ฏธํฐ์
๋๋ค. Matplotlib์ `rcParams`์์ `font.family`์ `font.size`๊ฐ ํ๋ ์ผ๊ณผ ๋์ผํ ์ผ์ ์ํํฉ๋๋ค.
```
pythonfrom matplotlib import rcParamsrcParams['font.family'] = 'Malgun Gothic'rcParams['font.size'] = 18
```
Matplotlib์ `rcParams`์์์ฒ๋ผ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ ์ ๋ฐ์ ์กฐ์ ํ๊ณ ์ถ๋ค๋ฉด `rc` ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
##### rc
`rc` ํ๋ผ๋ฏธํฐ๋ ์ถ(axes), ๊ทธ๋ฆฌ๋(grid), ๋๊ธ(ticks), ๊ธ๊ผด(font) ๋ฑ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ์ ์ ๋ฐ์ ์กฐ์ ํ๋ ํ๋ผ๋ฏธํฐ์
๋๋ค. `plotting_context()` ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ฌ ๊ทธ๋ํ์ ์ ์ฉ๋๊ณ ์๋ ์ค์ ๊ฐ์ ์ ์ ์์ต๋๋ค. `rc` ํ๋ผ๋ฏธํฐ์์ ์ฌ์ฉํ ์ ์๋ ์ค์ ๊ฐ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.plotting_context(){'axes.facecolor': 'white','axes.edgecolor': 'black','axes.grid': False,'axes.axisbelow': 'line','axes.labelcolor': 'black','figure.facecolor': 'white','grid.color': '#b0b0b0','grid.linestyle': '-','text.color': 'black','xtick.color': 'black','ytick.color': 'black','xtick.direction': 'out','ytick.direction': 'out','lines.solid_capstyle': <CapStyle.projecting: 'projecting'>,'patch.edgecolor': 'black','patch.force_edgecolor': False,'image.cmap': 'viridis','font.family': ['sans-serif'],'font.sans-serif': ['DejaVu Sans','Bitstream Vera Sans','Computer Modern Sans Serif','Lucida Grande','Verdana','Geneva','Lucid','Arial','Helvetica','Avant Garde','sans-serif'],'xtick.bottom': True,'xtick.top': False,'ytick.left': True,'ytick.right': False,'axes.spines.left': True,'axes.spines.bottom': True,'axes.spines.right': True,'axes.spines.top': True}
```
์ฌ๊ธฐ์ `axes.spines`์ ๊ทธ๋ํ์ ์ถ์ ๋ํ๋ด๊ฑฐ๋ ์จ๊ธฐ๋ ์ต์
์
๋๋ค. ๋ฐ๋ก ์ค์ ํ์ง ์์ผ๋ฉด Seaborn์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์(top), ์๋(bottom), ์ผํธ(left), ์ค๋ฅธํธ(right) ์ด 4๊ฐ์ ์ถ์ ๋ณด์ฌ์ค๋๋ค. ๋ง์ผ ์์ชฝ ์ถ๊ณผ ์ค๋ฅธ์ชฝ ์ถ์ ์จ๊ธฐ๊ณ ์ถ๋ค๋ฉด `despine()` ํจ์๋ฅผ ์ด์ฉํด๋ ๋ฉ๋๋ค. `despine()` ํจ์๋ ๋ฐ๋์ ๊ทธ๋ํ ํจ์ ๋ค์์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
```
pythonsns.countplot(...)sns.despine()
```
๋ง์ผ ํน์ ์ถ์ ์จ๊ธฐ๊ณ ์ถ๋ค๋ฉด ์๋์ ๊ฐ์ด ์จ๊ธฐ๊ณ ์ถ์ ๋ฐฉํฅ์ ํ๋ผ๋ฏธํฐ์ `True` ๊ฐ์ ์ ๋ฌํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.countplot(...)sns.despine(left=True, bottom=True)
```
#### set\_style
`set_style()` ํจ์๋ก๋ ๊ทธ๋ํ ์ ๋ฐ์ ์ ์ฉ๋ ํ
๋ง์ ๊ทธ๋ํ์ ์ธ๋ถ ์ฌํญ์ ์กฐ์ ํ ์ ์์ต๋๋ค.
```
pythonrc={'grid.color': '.5', 'grid.linestyle': ':'}sns.set_style('whitegrid', rc=None)
```
#### set\_context
`set_context()` ํจ์๋ก๋ ๊ทธ๋ํ์ ์ค์ผ์ผ์ ์ง์ ํ ์ ์์ต๋๋ค.
```
pythonsns.set_context('notebook', font_scale=1.25, rc={'grid.color': '.6'})
```
#### set\_palette
`set_palette()` ํจ์๋ก๋ ๊ทธ๋ํ์ ์์ ํ๋ ํธ๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
```
pythonsns.set_palatte('colorblind')
```
### ๊ทธ๋ํ๋ณ ์คํ์ผ๋ง
๊ทธ๋ํ๋ณ ์คํ์ผ๋ง์ ํ๋ ค๋ฉด `set()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
#### ์ถ ๋ฒ์ ์ ํํ๊ธฐ: xlim, ylim
Seaborn์์ x์ถ๊ณผ y์ถ์ ๋ฒ์๋ฅผ ์ค์ ํ๋ ค๋ฉด `xlim`, `ylim` ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.countplot(...).set(xlim=(1, 10), ylim=(0, 20))
```
#### ์ถ ๋ผ๋ฒจ ์จ๊ธฐ๊ธฐ: xlabel, ylabel
Seaborn์์ ์ถ์ ์๋ ๋ผ๋ฒจ์ ์จ๊ธฐ๋ ค๋ฉด `xlabel`, `ylabel` ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonax = sns.heatmap(...)ax.set(xlabel="", ylabel="")
```
#### ์ถ ์์น ๋ฐ๊พธ๊ธฐ
์ถ ์์น๋ฅผ ์กฐ์ ํ๋ ค๋ฉด `ax.axis.tick_top` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonax = sns.heatmap(...)ax.xaxis.tick_top()ax.yaxis.tick_left
```
#### ๊ทธ๋ํ ํฌ๊ธฐ ์กฐ์ ํ๊ธฐ
Seaborn์์ ๊ฐ๋ณ ๊ทธ๋ํ์ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ ค๋ฉด `rc` ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.barplot(...)sns.set(rc={'figure.figsize':(10,7)})
```
์ค์น์ ๊ธฐ๋ณธ ํ๊ฒฝ ์ค์ ์ ๋ชจ๋ ๋ง์ณค๋ค๋ฉด ์ด์ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉ(loading)ํด์ค๊ฒ ์ต๋๋ค.
### ๋ฐ์ดํฐ
Seaborn์์๋ ๋ฐ์ดํฐ๋ฅผ ์ธ๋ถ์์ ๊ฐ์ ธ์ฌ ์๋ ์๊ณ , ๋ด์ฅ ๋ฐ์ดํฐ(built-in data)๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
#### 1\) ๋ฐ์ดํฐ ์ ํ
##### ์ธ๋ถ ๋ฐ์ดํฐ
์ธ๋ถ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ฉํ๋ ค๋ฉด pandas๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. CSV ํ์ผ๊ณผ ์์
ํ์ผ์ DataFrame ๊ฐ์ฒด๋ก ๋ถ๋ฌ์ค๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonimport pandas as pddf = pd.read_csv('data.csv')df = pd.read_excel('data.xlsx')
```
pandas์์ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉํ๋ ์์ธํ ๋ฐฉ๋ฒ์ [Python pandas ๋ฐ์ดํฐ ์์ฑ, ๋ก๋ฉ๊ณผ ์ ์ฅ, ์์ธ ๊ด๋ฆฌํ๋ ๋ฒ](https://www.snugarchive.com/blog/python-pandas-guide-1/)์์ '๋ก๋ฉ ๋ฐ ์ ์ฅ' ํธ์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฌ๊ธฐ์๋ Seaborn์ ๋ด์ฅ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ฒ ์ต๋๋ค.
##### ๋ด์ฅ ๋ฐ์ดํฐ
Seaborn์๋ ๋ค์ํ ๋ด์ฅ ๋ฐ์ดํฐ์
์ด ์์ต๋๋ค. ํจํค์ง ๋ด์ ์ด๋ค ๋ด์ฅ ๋ฐ์ดํฐ์
์ด ์๋์ง ์์๋ณด๋ ค๋ฉด `get_dataset_names()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.get_dataset_names()['anagrams', 'anscombe', 'attention', 'brain_networks', 'car_crashes', 'diamonds','dots', 'exercise', 'flights', 'fmri', 'gammas', 'geyser', 'iris', 'mpg','penguins', 'planets', 'taxis', 'tips', 'titanic']
```
์ด ๋ฐ์ดํฐ์
์ค์์ ๋ช ๊ฐ์ง๋ฅผ ์ ํํด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
#### 2\) ๋ฐ์ดํฐ ๋ก๋ฉ
Seborn์ ๋ด์ฅ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉํ๋ ค๋ฉด `load_dataset()` ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. pandas๋ฅผ ์ด์ฉํด ๊ฐ์ ธ์จ ์ธ๋ถ ๋ฐ์ดํฐ์ ๋ง์ฐฌ๊ฐ์ง๋ก `load_dataset()` ํจ์๋ก ๋ถ๋ฌ์จ ๋ฐ์ดํฐ ํ์๋ DataFrame ๊ฐ์ฒด์
๋๋ค. ์ฌ๊ธฐ์๋ ๋ค์ ๋ฐ์ดํฐ์
์ ๋ถ๋ฌ์ ๋ณด๊ฒ ์ต๋๋ค.
```
pythondf_titanic = sns.load_dataset('titanic')df_iris = sns.load_dataset('iris')df_penguins = sns.load_dataset('penguins')df_tips = sns.load_dataset('tips')df_diamonds = sns.load_dataset('diamonds')df_planets = sns.load_dataset('planets')
```
#### 3\) ๋ฐ์ดํฐ ๊ตฌ์กฐ ํ์
๋ฐ์ดํฐ์
์ด ์ ์ค๋น๋์๋ค๋ฉด ๋ฐ์ดํฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ๋จํ ์ดํด๋ณด๊ฒ ์ต๋๋ค. pandas์์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ๋ ์์ธํ ๋ฐฉ๋ฒ์ ์๊ณ ์ถ์ผ์๋ค๋ฉด [Python pandas ๋ฐ์ดํฐ ํ์ธ, ์ ๋ ฌ, ์ ํํ๋ ๋ฒ](https://www.snugarchive.com/blog/python-pandas-guide-2/)์์ "๋ฐ์ดํฐ ํ์ธ" ๋ถ๋ถ์ ์ฐธ์กฐํด ์ฃผ์ธ์.
```
pythondf.shapedf.head()df['class']
```
๋ฐ์ดํฐ์
์ด ์ ์ค๋น๋์๋ค๋ฉด ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์๊ฐํ๋ฅผ ํด๋ณด๊ฒ ์ต๋๋ค. ๊ธฐ๋ณธํธ์์ ์๊ฐํํ ๋ฐ์ดํฐ๋ 1์ฐจ์ ๋ฐ์ดํฐ์
๋๋ค. 1์ฐจ์ ๋ฐ์ดํฐ๋ ์์ฑ(attribute)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ์
๋๋ค. Numpy ๋ฐฐ์ด์์ ์์๋ฅผ ํ ์ค๋ก ๋ํ๋ผ ์ ์๋ ๋ฐ์ดํฐ, ์์
์์ ์ด(columns)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ, ๋
๋ฆฝ๋ณ์(independent variable) ๋๋ ๋ณ๋(variate)์ด 1๊ฐ์ธ ๋ฐ์ดํฐ๋ผ๊ณ ๋ ๋ณผ ์ ์์ต๋๋ค.
1์ฐจ์ ๋ฐ์ดํฐ๋ ์์นํ๊ณผ ๋ฒ์ฃผํ์ผ๋ก ๋๋ฉ๋๋ค. ์์นํ์ ๋ณ์๊ฐ ์ค์ซ๊ฐ์ธ ์ฐ์์ ๋ณ์(continous variables)์ ์ ์ซ๊ฐ์ธ ์ด์ฐ์ ๋ณ์(discrete variables)์ธ ๋ฐ์ดํฐ์
๋๋ค. ๋ฒ์ฃผํ์ ๋ณ์๊ฐ ์นดํ
๊ณ ๋ฆฌ(category)์ฒ๋ผ ๋ถ๋ฅ๋ ์ง์ ๋ณ์(qualitative variables)๋ฅผ ์๋ฏธํฉ๋๋ค. ๊ทธ๋ผ 1์ฐจ์ ๋ฒ์ฃผํ ๋ฐ์ดํฐ๋ถํฐ ์๊ฐํํด ๋ณด๊ฒ ์ต๋๋ค.
## 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ๋ฒ์ฃผํ
์ผ๋ณ๋ ๋ฒ์ฃผํ ์๋ฃ๋ฅผ ์๊ฐํํ๋ ๋ฐ๋ ๋ง๋๊ทธ๋ํ(bar graph)์ ํ์ด ์ฐจํธ(pie chart)๋ฅผ ์ด์ฉํฉ๋๋ค. ๋ค๋ง, Seaborn์๋ ํ์ด ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ธฐ๋ฅ์ด ์์ต๋๋ค. ํ์ด ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด Matplotlib์ ์ด์ฉํด์ผ ํฉ๋๋ค. ์์ธํ ๋ฐฉ๋ฒ์ ํ์ด์ฌ Matplotlib ์ฌ์ฉ๋ฒ(์์ )์ ์ฐธ์กฐํด ์ฃผ์ธ์. ์ฌ๊ธฐ์๋ Seaborn์ผ๋ก ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
### 1\) ๋น๋ ๋ง๋๊ทธ๋ํ: countplot()
Seaborn์ผ๋ก ๋ฒ์ฃผํ ๋ฐ์ดํฐ์ ๋ํ ๋น๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ํจ์๋ `countplot()`์
๋๋ค. ์ด ํจ์๋ ๊ฐ ๋ฒ์ฃผ์ ์ํ๋ ๋ฐ์ดํฐ์ ๋น๋(๊ฐ์)๋ฅผ ๋ง๋์ ๋์ด๋ก ํ์ํฉ๋๋ค. ๋จผ์ ์์ง ๋น๋ ๋ง๋๊ทธ๋ํ๋ถํฐ ๊ทธ๋ ค๋ณด๊ฒ ์ต๋๋ค.
#### ์์ง ๋น๋ ๋ง๋๊ทธ๋ํ
##### ๊ธฐ๋ณธ
Seaborn์ผ๋ก ์์ง ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ธฐ๋ณธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.countplot(df_titanic['class'])sns.countplot(x=df_titanic['class'])sns.countplot(x='class', data=df_titanic)
```
์ฝ๋1์ ์๋์ ๊ฐ์ด ์ต์
์ ์ถ๊ฐํ ์๋ ์์ต๋๋ค.
> - color: ๋ง๋ ์ ์ง์
> - edgecolor: ๋ง๋ ํ
๋๋ฆฌ์ ์ง์
> - palette: ๊ทธ๋ํ ์ ์ง์
> - alpha: ๊ทธ๋ํ ํฌ๋ช
๋ ์ง์
> - linewidth: ๊ทธ๋ํ ๊ตต๊ธฐ ์ง์
`palette`์ ๋ค์ํ ์ต์
์ [Seaborn ๊ณต์ ํํ์ด์ง color palette](https://seaborn.pydata.org/tutorial/color_palettes.html)์์ ํ์ธํ์ค ์ ์์ต๋๋ค.
```
pythonsns.countplot(x='class', data=df_titanic, color='skyblue')sns.countplot(x='class', data=df_titanic, palette='Set3')sns.countplot(x='class', data=df_titanic,facecolor=(0, 0, 0, 0),linewidth=5,edgecolor=sns.color_palette('dark', 3))
```
์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.

countplot ํจ์๋ก ๊ทธ๋ฆฐ ์์ง ๋ง๋๊ทธ๋ํ
`countplot()` ํจ์ ์ธ์๋ `catplot()` ํจ์๋ฅผ ์ด์ฉํ ์๋ ์์ต๋๋ค. `catplot()` ํจ์๋ ์์นํ ๋ณ์์ ๋ฒ์ฃผํ ๋ณ์์ ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ ํจ์์ด์ง๋ง, `kind='count'` ์ต์
์ ์ถ๊ฐํ๋ฉด ๋น๋๋ฅผ ๋ํ๋ด๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆด ์ ์์ต๋๋ค (์ฝ๋1๊ณผ ๋์ผ).
```
pythonsns.catplot(x='class', kind='count', data=df_titanic)
```

catplot() ํจ์๋ก ๊ทธ๋ฆฐ ์์ง ๋ง๋๊ทธ๋ํ
##### ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
๋ง์ผ ๋ง๋๊ทธ๋ํ๋ฅผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ๋ค๋ฉด `order` ํ๋ผ๋ฏธํฐ์ `df.value_counts().index` ์ฝ๋๋ฅผ ๋ํด์ฃผ๋ฉด ๋ฉ๋๋ค. `df.value_counts().index`๋ ๋ณ์์ ๊ฐ์ ๋น๋๊ฐ ๋์ ์์ผ๋ก ์ ๋ ฌํด์ค๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.countplot(x='class', data=df_titanic,order=df_titanic['class'].value_counts().index)
```

๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ ์์ง ๋ง๋๊ทธ๋ํ
##### ์์ฝ๊ฐ ํ์
๊ฐ ๋ง๋ ์์ ์์ฝ๊ฐ์ ์ซ์๋ก ํ์ํ๊ณ ์ถ๋ค๋ฉด ๊ทธ๋ํ๋ฅผ ๋ณ์์ ํ ๋นํ ๋ค `ax.bar_label(ax.containers[0])` ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค.
```
pythonax = sns.countplot(df_titanic['class'])ax.bar_label(ax.containers[0])
```

์์ฝ๊ฐ์ ํ์ํ ์์ง ๋ง๋๊ทธ๋ํ
##### ์๊น ๊ฐ์กฐ
ํน์ ๋ง๋์ ์๊น์ ๊ฐ์กฐํ ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๊ฐ์ฅ ๋น๋์๊ฐ ๋์ ๋ง๋๋ง ๋ค๋ฅธ ์์ผ๋ก ํ์ํ๊ณ ์ถ๋ค๋ฉด `numpy`์ `barplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonvalues = np.array(df_titanic['class'].value_counts())idx = np.array(df_titanic['class'].value_counts().index)palette = ['skyblue' if (x == max(values)) else 'lightgrey' for x in values]sns.barplot(x=idx, y=values, palette=palette)
```

ํน์ ๋ง๋๋ฅผ ๋ค๋ฅธ ์์ผ๋ก ๊ฐ์กฐํ ์์ง ๋ง๋๊ทธ๋ํ
#### ์ํ ๋น๋ ๋ง๋๊ทธ๋ํ
Seaborn์ผ๋ก ์ผ๋ณ๋ ๋ฒ์ฃผํ ์๋ฃ์ ๋ํ ๊ฐ๋ก ๋น๋ ๋ง๋๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด `countplot()` ํจ์์ x ๋งค๊ฐ๋ณ์ ๋์ y ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. ์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.countplot(y='class', data=df_titanic)sns.catplot(y='class', kind='count', palette='ch:.25', data=df_titanic)
```

countplot() ํจ์๋ก ๊ทธ๋ฆฐ ์ํ ๋ง๋๊ทธ๋ํ
์ง๊ธ๊น์ง ์ผ๋ณ๋ ๋ฒ์ฃผํ ๋ฐ์ดํฐ๋ฅผ ๋ง๋๊ทธ๋ํ๋ก ์๊ฐํํ๋ ๋ฒ์ ์ดํด๋ณด์์ต๋๋ค. ์ด๋ฒ์๋ ์ผ๋ณ๋ ์์นํ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
### 2\) ์์ ๊ทธ๋ฆผ
์์ ๊ทธ๋ฆผ(๋์์ด: box plot, ๋ฐ์ค ํ๋กฏ, ๋ฐ์ค ๊ทธ๋ํ, ์์ ๊ทธ๋ํ)์ ๋ฐ์ดํฐ์ 5๊ฐ์ง ํต๊ณ๋(์ต์๊ฐ, ์ 1 ์ฌ๋ถ์, ์ 2์ฌ๋ถ์, ์ 3์ฌ๋ถ์, ์ต๋๊ฐ)์ ๋ํ๋ด๋ ๊ทธ๋ํ์
๋๋ค. ์์๊ทธ๋ฆผ์ ๋ฐ์ดํฐ์ ์ ์ฒด์ ์ธ ๋ถํฌ์ ์ด์์น๋ฅผ ํ์ธํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
#### boxplot()
์์๊ทธ๋ฆผ์ ๊ทธ๋ฆฌ๋ ค๋ฉด `boxplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.boxplot(data=df_iris, x='sepal_length')
```

boxplot ํจ์๋ก ๊ทธ๋ฆฐ ์์ ์์ผ ๊ทธ๋ฆผ
`catplot()` ํจ์์ `kind='box'` ์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
```
pythonsns.catplot(data=df_iris, x='sepal_length', kind='box')
```
#### boxenplot()
๋ฐ์ดํฐ์
์ ๋ฐ์ดํฐ ๋ฒ์๊ฐ ํด ๊ฒฝ์ฐ์๋ `boxenplot()`์ ์ฌ์ฉํ ์ ์์ต๋๋ค. `boxenplot()`์ ๋ฐ์ดํฐ๋ฅผ ๋ ๋ง์ ๋ถ์๋ก ๋๋์ด ํฌ๊ธฐ๊ฐ ํฐ ๋ฐ์ดํฐ์
์ ๋ฒ์ฃผ๋ฅผ ์์๊ทธ๋ฆผ์ผ๋ก ํํํด ์ค๋๋ค.
```
pythonsns.boxenplot(data=df_diamonds, x='price')
```

boxenplot() ํจ์๋ก ๊ทธ๋ฆฐ ์์๊ทธ๋ฆผ
`catplot()` ํจ์์ `kind='boxen'` ์ต์
์ ์ฌ์ฉํด๋ ๋ฉ๋๋ค.
```
pythonsns.catplot(data=df_diamonds, x='price', kind='boxen')
```
#### violinplot()
์์ ๊ทธ๋ฆผ๊ณผ ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ํฉ์ณ์ ๊ทธ๋ฆฌ๋ ค๋ฉด `violinplot()`์ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.violinplot(data=df_iris, x='sepal_length')
```

violinplot() ํจ์๋ก ๊ทธ๋ฆฐ ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ
```
pythonsns.catplot(data=df_iris, x='sepal_length', kind='violin')
```
์ ๊ทธ๋ํ์์ ๊ฐ์ด๋ฐ ํฐ์ ์ ์ ์ค์๊ฐ(median)์ ๋ํ๋
๋๋ค. ๋ํ, ํฐ์ ์ ์ ๋๋ฌ์ผ ๋๊บผ์ด ์ ์ ์ฌ๋ถ์ ๋ฒ์๋ฅผ, ๋๊บผ์ด ์ ์์ ์ ๋์ผ๋ก ์ด์ด์ง๋ ์์ ์ ์ 95% ์ ๋ขฐ ๊ตฌ๊ฐ์ ๋ํ๋
๋๋ค.
## 1์ฐจ์ ๋ฐ์ดํฐ ์๊ฐํ: ์์นํ
์์นํ ๋ฐ์ดํฐ๋ ๋ถํฌ๋ฅผ ๋ณด๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
### 1\) ์ ๊ทธ๋ํ: stripplot(), swarmplot()
์ ๊ทธ๋ํ(๋์์ด: dot graph, strip chart, ์ ๋ํ)๋ ๋ฐ์ดํฐ์ ์ค์ ์์น๋ฅผ ์ (dots)์ผ๋ก ๋ณด์ฌ์ค๋๋ค. ์ ๊ทธ๋ํ๋ ๋ฐ์ดํฐ์ ์ค์ ์์น๋ฅผ ํ์
ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค. Seaborn์ผ๋ก ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด `stripplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.stripplot(data=df_iris, x='sepal_length')sns.stripplot(x=df_iris['sepal_length'])
```

stripplot() ํจ์๋ก ๊ทธ๋ฆฐ ์ ๊ทธ๋ํ
`catplot()` ํจ์์ `kind='strip'` ์ต์
์ ์ถ๊ฐํด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
```
pythonsns.catplot(data=df_iris, x='sepal_length', kind='strip')
```
๋ค๋ง ์ ๊ทธ๋ํ์ ๊ฒฝ์ฐ ํํํ ๋ฐ์ดํฐ ํฌ์ธํธ๋ค์ด ๋ง์์ง๋ฉด ์ ๋ค์ด ๊ฒน์ณ ๋ณด์ผ ์ ์์ต๋๋ค. ์ด๋๋ `swarmplot()`์ ์ด์ฉํด ์๋ฃ๋ฅผ ํฉํธ๋ ค์(jittering) ์ ์ฌ์ด์ ๊ฐ๊ฒฉ์ ์กฐ์ ํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.swarmplot(data=df_iris, x='sepal_length')
```

swarmplot() ํจ์๋ก ํํธ๋ ค ๊ทธ๋ฆฐ ์ ๊ทธ๋ํ
### 2\) ์ ๋ถ๊ทธ๋ํ: rugplot()
์ ๋ถ๊ทธ๋ํ(rug plot) ๋๋ ๋ฌ๊ทธ ํ๋กฏ์ ๋ชจ๋ ๋ฐ์ดํฐ ํฌ์ธํธ๋ฅผ ์ถ ์์ ์์ ์ ๋ถ(rug)์ผ๋ก ๋ํ๋ธ ๊ทธ๋ํ์
๋๋ค. ์ ๋ถ ๊ทธ๋ํ์ ๊ฐ ์ ๋ถ์ ์ค์ ๋ฐ์ดํฐ๋ค์ ์์น๋ฅผ ๋ณด์ฌ์ค๋๋ค. ์ ๋ถ๋ค์ด ์ด์ดํ ์์์๋ก ๋ฐ์ดํฐ๊ฐ ๋ฐ์ง๋์ด ์์์ ์๋ฏธํฉ๋๋ค. ์ฃผ๋ณ ๋ถํฌ(marginal distribution)์ ๋ํ๋ด๋ฉฐ ์ฃผ๋ก ๋ค๋ฅธ ๊ทธ๋ํ์ ํจ๊ป ์ฌ์ฉ๋ฉ๋๋ค.
๋ฌ๊ทธ ํ๋กฏ์ ๊ทธ๋ฆฌ๋ ค๋ฉด `rugplot()` ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. `displot()` ํจ์์ `rug=True` ์ต์
์ ์ฃผ์ด๋ ๋ฉ๋๋ค. ๋ฌ๊ทธ๋ฅผ ์ธ๋ถ์ ์ผ๋ก ์กฐ์ ํด์ผ ํ ๋๋ `displot()` ํจ์๋ณด๋ค๋ `rugplot()` ํจ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
> - height: ์ ๋ถ ๊ธธ์ด ์ง์
> - clip\_on: ์ ๋ถ ์ถ ๋ฐ์ ๊ทธ๋ฆฌ๊ธฐ ์ง์
> - lw: ์ ๋ถ ์๊ธฐ ์ง์
> - alpha: ์ ๋ถ ํฌ๋ช
๋ ์ง์
```
pythonsns.displot(data=df_tips, x='total_bill', rug=True)sns.rugplot(data=df_tips, x='total_bill', height=.1)sns.rugplot(data=df_tips, x='total_bill', height=-.02, clip_on=False)sns.rugplot(data=df_diamonds, x='carat', lw=1, alpha=.005)
```

rugplot() ํจ์๋ก ๊ทธ๋ฆฐ ๋ค์ํ ์ ๋ถ๊ทธ๋ํ
๋ฐ์ดํฐ๊ฐ ๋ง์์ง ๊ฒฝ์ฐ ์ ๊ทธ๋ํ ๋๋ ์ ๋ถ๊ทธ๋ํ๋ง์ผ๋ก ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ํ๋์ ํ์
ํ๊ธฐ ์ด๋ ต์ต๋๋ค. ๋ฐ์ดํฐ ํฌ์ธํธ๊ฐ ์๋ก ๊ฒน์ณ ๋ณด์ด๊ธฐ ๋๋ฌธ์
๋๋ค. ์ด๋ ์ฌ์ฉํ๋ฉด ์ข์ ๋๊ตฌ๊ฐ ํ์คํ ๊ทธ๋จ์
๋๋ค.
### 3\) ํ์คํ ๊ทธ๋จ: histplot()
ํ์คํ ๊ทธ๋จ(histogram)์ ์์นํ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ๊ตฌ๊ฐ๋ณ ๋น๋์๋ก ํํํ ๊ทธ๋ํ์
๋๋ค. ์ฆ, ๋ฐ์ดํฐ๋ฅผ ๋ช ๊ฐ์ ๊ตฌ๊ฐ์ผ๋ก ๋๋ ํ ๊ฐ ๊ตฌ๊ฐ์ ํฌํจ๋ ๋ฐ์ดํฐ์ ๊ฐ์ ๋๋ ๋์(frequency)๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ๋ํ๋ธ ๊ฒ์
๋๋ค. ๋ฐ์ดํฐ๊ฐ ๊ตฌ๊ฐ์ผ๋ก ์ฒ๋ฆฌ๋๊ธฐ ๋๋ฌธ์, ๋ฒ์๊ฐ ๋์ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ํ์
ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
#### ๊ธฐ๋ณธ
Seaborn์์ ๋ณ์๊ฐ 1๊ฐ์ธ ๋จ๋ณ๋ ํ์คํ ๊ทธ๋จ์ ๋ง๋ค๋ ค๋ฉด `histplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. ์ปค๋๋ฐ๋์ถ์ (Kernel Density Estimation, KDE) ๋ฐฉ๋ฒ์ผ๋ก ์ค๋ฌด๋ฉ(smoothing)ํ ํ์คํ ๊ทธ๋จ์ ํ๋ฅ ๋ฐ๋ํจ์(Probability Density Function, PDF)๋ฅผ ํจ๊ป ๊ทธ๋ฆฌ๊ณ ์ถ๋ค๋ฉด `kde=True` ์ต์
์ ์ฃผ๋ฉด ๋ฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.histplot(df_penguins, x='flipper_length_mm')sns.histplot(df_penguins['flipper_length_mm'], kde=True)
```
`displot()` ํจ์๋ฅผ ์ด์ฉํด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค. `displot()` ํจ์์ ์ด๊ธฐ ๊ธฐ๋ณธ ์ค์ ์ `kind='hist'`์
๋๋ค. `displot()` ํจ์์ `kind` ์ต์
์ ๋ฐ๋ก ์ง์ ํ์ง ์์ผ๋ฉด ํ์คํ ๊ทธ๋จ์ ๊ทธ๋ฆฝ๋๋ค.
```
pythonsns.displot(df_penguins, x='flipper_length_mm')sns.displot(df_penguins['flipper_length_mm'], kde=True)
```

histplot() ํจ์๋ก ๋ง๋ ๋จ๋ณ๋ ํ์คํ ๊ทธ๋จ
```
pythonsns.displot(df_diamonds, x='carat', kde=True)
```

ํ์คํ ๊ทธ๋จ๊ณผ ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์
#### ํน์ ์กฐ๊ฑด
pandas๋ฅผ ์ด์ฉํ๋ฉด ๋ฐ์ดํฐ๋ฅผ ์กฐ๊ฑด๋ณ๋ก ์ ํํ ์ ์์ต๋๋ค. ๋ค์ ์ฝ๋๋ 'species'๊ฐ 'Adelie'์ธ ํญ๊ท์ 'flipper\_length\_mm'๋ฅผ ๊ด์ธกํ ๊ฐ์ ๋ํด ํ์คํ ๊ทธ๋จ์ ๊ทธ๋ฆฌ๋ ์์ ์
๋๋ค.
```
pythonsns.histplot(df_penguins[df_penguins['species'] == 'Adelie']['flipper_length_mm'])
```
#### ๋ฑ๊ธ ์์ ๋ฑ๊ธ ํญ: bins, binwidth
ํ์คํ ๊ทธ๋จ์์๋ ๋ฑ๊ธ์ ์ ๋๋ ๋ฑ๊ธ์ ํญ์ ๋ฐ๋ผ ๊ทธ๋ํ์ ๋ชจ์์ด ๋ฌ๋ผ์ง๋๋ค. ๋ฑ๊ธ์ ์๋ `bins` ์ต์
์ผ๋ก, ๋ฑ๊ธํญ์ `binwidth` ์ต์
์ผ๋ก ์ง์ ํฉ๋๋ค.
```
pythonsns.histplot(df_penguins, x='flipper_length_mm', bins=10)sns.histplot(df_penguins, x='flipper_length_mm', binwidth=3)
```

๊ตฌ๊ฐ์ ์์ ๊ตฌ๊ฐ์ ํญ์ ์กฐ์ ํ ํ์คํ ๊ทธ๋จ
#### ๋ฑ๊ธ๋ช
, ๋ฑ๊ธ๋ช
์์น, ๋ฑ๊ธ ์ฌ์ด ๊ณต๊ฐ: bins=๋ฆฌ์คํธ, discrete=True, shrink
ํ์คํ ๊ทธ๋จ์ ์ฃผ๋ก ์ฐ์์ ์๋ฃ๋ฅผ ์๊ฐํํ ๋ ์ฐ์ด์ง๋ง, ์ข
์ข
์ด์ฐ์ ์๋ฃ(discrete data)๋ฅผ ์๊ฐํํ๋ ๋ฐ๋ ์ฌ์ฉํฉ๋๋ค. ์ด์ฐ์ ์๋ฃ๋ ๋ง๋๊ทธ๋ํ๋ก ํํํฉ๋๋ค. ๋จ, ํ์คํ ๊ทธ๋จ์ ๋ง๋๊ทธ๋ํ๋ก ๋ณํํ ๋๋ ๋ค์๊ณผ ๊ฐ์ ์กฐ์ ์ด ํ์ํฉ๋๋ค.
> - ๋ฑ๊ธ ๋ช
์
> - ๋ฑ๊ธ๋ช
์ด ๋ง๋์ ์ค์์ ์ค๋๋ก ์์น
> - ๋ณ์๊ฐ ์ฐ์์ ์ด์ง ์๊ณ ์ด์ฐ์ ์ด๋ผ๋ ๊ฒ์ ์๋ ค์ฃผ๊ธฐ ์ํด ๋ฑ๊ธ๊ณผ ๋ฑ๊ธ ์ฌ์ด์ ์ฌ์ ๋๊ธฐ
์ด๋ฅผ ๋์์ฃผ๋ ์ต์
์ด ๊ฐ๊ฐ `bins=๋ฆฌ์คํธ`, `discrete=True`, `shrink`์
๋๋ค. ๊ฐ ์ต์
์ด ์ํํ๋ ์ผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
> - bins=๋ฆฌ์คํธ: ๊ทธ๋ํ์ x์ถ์ ๋ช
์ํ ๋ฑ๊ธ์ ์ง์ ์ง์
> - discrete=True: ๊ฐ ๋ฑ๊ธ์ด ๋ง๋ ์ค์์ ์ค๋๋ก ์์น
> - shrink: ๊ฐ ๋ง๋ ์ฌ์ด์ ๊ณต๊ฐ์ ๋ง๋ จ
์์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.histplot(df_tips, x='size')sns.histplot(df_tips, x='size', bins=[1, 2, 3, 4, 5, 6, 7])sns.histplot(df_tips, x='size', discrete=True)sns.histplot(df_tips, x='size', discrete=True, shrink=.8)
```

๋ฑ๊ธ๋ช
๊ณผ ๋ฑ๊ธ๋ช
์ ์์น, ๋ฑ๊ธ ์ฌ์ด์ ๊ณต๊ฐ์ ์กฐ์ ํ ํ์คํ ๊ทธ๋จ
๋จ, ํ์คํ ๊ทธ๋จ์ ๋ฐ์ดํฐ์ ์ฐ์์ ํน์ฑ์ ์จ์ ํ ํํํ์ง ๋ชปํ๋ ๋จ์ ์ด ์์ต๋๋ค. ๊ตฌ๊ฐ์ ํฌ๊ธฐ์ ์์์ ์ ๋ฐ๋ผ ๋ถํฌ์ ๋ชจ์์ด ํฌ๊ฒ ๋ฌ๋ผ์ง ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ฐ์ํ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ๋ถ๋๋ฌ์ด ๊ณก์ ์ผ๋ก ํํํ๋ ๋ฐฉ๋ฒ์ด ์์๊น์?
### 4\) ์ปค๋๋ฐ๋์ถ์ ์ผ๋ก ๊ตฌํ ํ๋ฅ ๋ฐ๋ํจ์: kdeplot()
์ปค๋๋ฐ๋์ถ์ (Kernel density estimation)์ ์ฃผ์ด์ง ๋ฐ์ดํฐ ํฌ์ธํธ๋ค์ ๋ฐํ์ผ๋ก ์ ์ฒด ๋ฐ์ดํฐ์ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ๋ถ๋๋ฌ์ด ๊ณก์ ์ผ๋ก ์ถ์ ํ๋ ๊ธฐ๋ฒ์
๋๋ค. ์์นํ ๋ฐ์ดํฐ๋ฅผ ์ด์ฐ์ ์ผ๋ก ํํํ๋ ํ์คํ ๊ทธ๋จ ๋์ ๋งค๋๋ฌ์ด ๊ณก์ ์ผ๋ก ํํํ๊ณ ์ถ๋ค๋ฉด ์ปค๋ ๋ฐ๋ ํจ์(Kernel density function)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
#### ๊ธฐ๋ณธ
`kdeplot()` ํจ์๋ ๋จ๋ณ๋ ๋๋ ์ด๋ณ๋ ๋ฐ์ดํฐ์ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ์ถ์ ํ์ฌ ๊ทธ๋ฆฝ๋๋ค. ์ฌ๊ธฐ์ ํํ๋(bandwidth)์ ์กฐ์ ํ๋ ค๋ฉด `bw_adjust` ์ต์
์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. ์ด ์ต์
์ ํฌ๊ธฐ์ ๋ฐ๋ผ ํ๋ฅ ๋ฐ๋ํจ์์ ๋ถ๋๋ฌ์(smoothness) ์ ๋๊ฐ ๋ฌ๋ผ์ง๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
```
pythonsns.kdeplot(data=df_penguins, x='flipper_length_mm')sns.kdeplot(data=df_penguins, x='flipper_length_mm', bw_adjust=.25) ์ฝ๋ 2
```
`displot()` ํจ์์ `kind='kde'`์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
```
pythonsns.displot(penguins, x='flipper_length_mm', kind='kde')sns.displot(penguins, x='flipper_length_mm', kind='kde', bw_adjust=.25)
```

kdeplot ํจ์๋ก ๊ทธ๋ฆฐ ํ๋ฅ ๋ฐ๋ํจ์
๋ง์ผ `displot()` ํจ์์ `kde=True` ์ต์
์ ์ง์ ํ๋ฉด ํ์คํ ๊ทธ๋จ๊ณผ ์ปค๋๋ฐ๋์ถ์ ํจ์๋ฅผ ๋์์ ๊ทธ๋ฆด ์ ์์ต๋๋ค.
#### ๋ฒ์ ์ ํ: cut
์ฐ์์ ๋ณ์๊ฐ ๋ฌดํํ ์ปค์ง์ง ์๋ ๊ฒฝ์ฐ์๋ `cut`์ด๋ผ๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํด ์ ๋์ ๋ฐ์ดํฐ ํฌ์ธํธ ๋ฒ์๋ฅผ ์ ํํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
```
pythonsns.kdeplot(df_tips, x='total_bill', kind='kde')sns.kdeplot(df_tips, x='total_bill', kind='kde', cut=0)
```

์ ๋์ ๋ฐ์ดํฐ ํฌ์ธํธ ๋ฒ์๋ฅผ ์ ํํ ํ๋ฅ ๋ฐ๋ํจ์
#### ์์: shade
KDE ๋ฐ๋ ๊ณก์ ์๋์ ์์ ์ฑ์ฐ๊ณ ์ถ๋ค๋ฉด `shade=True` ์ต์
์ ์ฃผ๋ฉด ๋ฉ๋๋ค.
### 5\) ๊ฒฝํ์ ๋์ ๋ถํฌํจ์: edcfplot()
๊ฒฝํ์ ๋์ ๋ถํฌํจ์(empirical cumulative distribution function, ECDF)๋ n๊ฐ์ ๋ฐ์ดํฐ ํฌ์ธํธ ๊ฐ๊ฐ์์ 1/n ์ฉ ์ ํํ๋ ๊ณ๋จ ํจ์์
๋๋ค. ๊ฐ๋จํ CDF๋ผ๊ณ ๋ ํฉ๋๋ค. ECDF ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ฉด `ecdfplot()` ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค.
```
pythonsns.ecdfplot(df_penguins, x='flipper_length_mm')
```

ecdfplot() ํจ์๋ก ๊ทธ๋ฆฐ ๊ฒฝํ์ ๋์ ๋ถํฌํจ์
`displot()` ํจ์์ `kind='ecdf'` ์ต์
์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
```
pythonsns.displot(df_penguins, x='flipper_length_mm', kind='ecdf')
```
์ง๊ธ๊น์ง ํ์ด์ฌ Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก 1์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด์์ต๋๋ค. ๋ค์ ์๊ฐ์๋ [ํ์ด์ฌ ๋ฐ์ดํฐ ์๊ฐํ Seaborn ์ฌ์ฉ๋ฒ ์ฌํํธ](https://www.snugarchive.com/blog/python-data-visualization-seaborn-advanced/)์์ ๋ค์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํํ๋ ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ๋ชจ๋ ์๊ณ ๋ง์ผ์
จ์ต๋๋ค.
## ์ฐธ๊ณ ๋ฌธํ
- \[1\] ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค ์ค์ฟจ, ๏ฝขํ์ด์ฌํธ 5์ฅ ๋ฐ์ดํฐ ์๊ฐํ: Seaborn์ ์ฌ์ฉํ ๋ฐ์ดํฐ ๋ถํฌ ์๊ฐํ๏ฝฃ, ๋ฐ์ดํฐ ์ฌ์ด์ธ์ค ์ค์ฟจ, "<https://datascienceschool.net/>"
- \[2\] ์๋ฌดํผ์๋ผ๋ฒจ, ๏ฝข์ ๊ทํ(Normalization) ์ฝ๊ฒ ์ดํดํ๊ธฐ๏ฝฃ, ์๋ฌดํผ์๋ผ๋ฒจ, "<https://hleecaster.com/ml-normalization-concept/>"
- \[3\] ์๋ฌดํผ์๋ผ๋ฒจ, ๏ฝข\[seaborn\] ๋ฐ์ด์ฌ๋ฆฐ ํ๋กฏ๏ฝฃ, ์๋ฌดํผ์๋ผ๋ฒจ, "<https://hleecaster.com/python-seaborn-violinplot/>"
- \[4\] Codeacademy Team, ๏ฝขSeaborn Styling, Part 1: Figure Style and Scale๏ฝฃ, Codecademy, "<https://www.codecademy.com/article/seaborn-design-i>"
- \[5\] Codeacademy Team, ๏ฝขSeaborn Styling, Part 2: Color๏ฝฃ, Codecademy, "<https://www.codecademy.com/article/seaborn-design-ii>"
- \[6\] Mahbubul Alam, ๏ฝขSeaborn can do the job, then why Matplotlib?๏ฝฃ, Towards Data Science, "<https://towardsdatascience.com/seaborn-can-do-the-job-then-why-matplotlib-dac8d2d24a5f>"
- \[7\] Seaborn, ๏ฝขseaborn: statistical data visualization๏ฝฃ, Seaborn, "<https://seaborn.pydata.org/index.html>"
- \[8\] StackOverflow, ๏ฝขHow to set a different color to the largest bar in a seaborn barplot๏ฝฃ, StackOverflow, "<https://stackoverflow.com/questions/31074758/how-to-set-a-different-color-to-the-largest-bar-in-a-seaborn-barplot>"
๋ค์ ๊ธ |
| Shard | 185 (laksa) |
| Root Hash | 15688750957463538185 |
| Unparsed URL | com,snugarchive!www,/blog/python-data-visualization-seaborn-basic/ s443 |