ℹ️ 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.4 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://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/ |
| Last Crawled | 2026-03-30 07:46:02 (12 days ago) |
| First Indexed | 2022-01-03 11:15:43 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Python >> Seaborn - (1) Seaborn을 활용한 다양한 그래프 그리기 | Hyemin Kim |
| Meta Description | matplotlib 차트들을 seaborn에서 구현하기 (scatterplot, barplot, lineplot, histogram, boxplot) |
| Meta Canonical | null |
| Boilerpipe Text | 0. Seaborn 개요
0-1. seaborn 에서만 제공되는 통계 기반 plot
0-2. 아름다운 스타일링
0-3. 컬러 팔레트
0-4. pandas 데이터프레임과 높은 호환성
1. Scatterplot
1-1. x, y, color, area 설정하기
1-2. cmap과 alpha
2. Barplot, Barhplot
2-1. 기본 Barplot 그리기
2-2. 기본 Barhplot 그리기
2-3. Barplot에서 비교 그래프 그리기
3. Line Plot
3-1. 기본 lineplot 그리기
3-2. 2개 이상의 그래프 그리기
3-3. 마커 스타일링
3-4. 라인 스타일 변경하기
4. Areaplot (Filled Area)
5.Histogram
5-1. 기본 Histogram 그리기
5-2. 다중 Histogram 그리기
6. Pie Chart
7. Box Plot
7-1. 기본 박스플롯 생성
7-2. 다중 박스플롯 생성
7-3. Box Plot 축 바꾸기
7-4. Outlier 마커 심볼과 컬러 변경
reference:
pyplot 공식 도튜먼트 살펴보기
seaborn 공식 도큐먼트 살펴보기
python
1
2
3
4
5
6
7
import
numpy
as
np
import
pandas
as
pd
import
matplotlib.pyplot
as
plt
from
IPython.display
import
Image
import
seaborn
as
sns
python
1
2
plt.rcParams[
"figure.figsize"
] = (
9
,
6
)
plt.rcParams[
"font.size"
] =
14
0. Seaborn 개요
seaborn은 matplotlib을 더 사용하게 쉽게 해주는 라이브러리다.
matplotlib으로 대부분의 시각화는 가능하지만, 다음과 같은 이유로 많은 사람들이
seaborn
을 선호한다.
비교:
matplotlib을 활용한 다양한 그래프 그리기
0-1. seaborn 에서만 제공되는 통계 기반 plot
python
1
tips = sns.load_dataset(
"tips"
)
(1) violinplot
python
1
2
3
sns.violinplot(x=
"day"
, y=
"total_bill"
, data=tips)
plt.title(
'violin plot'
)
plt.show()
(2) countplot
python
1
2
3
sns.countplot(tips[
'day'
])
plt.title(
'countplot'
)
plt.show()
(3) relplot
python
1
2
3
sns.relplot(x=
'tip'
, y=
'total_bill'
, data=tips)
plt.title(
'relplot'
)
plt.show()
(4) lmplot
python
1
2
3
sns.lmplot(x=
'tip'
, y=
'total_bill'
, data=tips)
plt.title(
'lmplot'
)
plt.show()
(5) heatmap
python
1
2
3
plt.title(
'heatmap'
)
sns.heatmap(tips.corr(), annot=
True
, linewidths=
1
)
plt.show()
0-2. 아름다운 스타일링
(1) default color의 예쁜 조합
seaborn의 최대 장점 중의 하나가 아름다운 컬러팔레트다.
스타일링에 크게 신경 쓰지 않아도 default 컬러가 예쁘게 조합해준다.
matplotlib VS seaborn
python
1
2
plt.bar(tips[
'day'
], tips[
'total_bill'
])
plt.show()
python
1
2
sns.barplot(x=
"day"
, y=
"total_bill"
, data=tips, palette=
"colorblind"
)
plt.show()
(2) 그래프 배경 설정
그래프의 배경 (grid 스타일)을 설정할 수 있음.
sns.set_style(’…’)
whitegrid: white background + grid
darkgrid: dark background + grid
white: white background (without grid)
dark: dark background (without grid)
python
1
2
3
sns.set_style(
'darkgrid'
)
sns.barplot(x=
"day"
, y=
"total_bill"
, data=tips, palette=
"colorblind"
)
plt.show()
python
1
2
3
sns.set_style(
'white'
)
sns.barplot(x=
"day"
, y=
"total_bill"
, data=tips, palette=
"colorblind"
)
plt.show()
0-3. 컬러 팔레트
자세한 컬러팔레트는
공식 도큐먼트
를 참고
python
1
2
3
4
5
6
sns.palplot(sns.light_palette((
210
,
90
,
60
), input=
"husl"
))
sns.palplot(sns.dark_palette(
"muted purple"
, input=
"xkcd"
))
sns.palplot(sns.color_palette(
"BrBG"
,
10
))
sns.palplot(sns.color_palette(
"BrBG_r"
,
10
))
sns.palplot(sns.color_palette(
"coolwarm"
,
10
))
sns.palplot(sns.diverging_palette(
255
,
133
, l=
60
, n=
10
, center=
"dark"
))
python
1
sns.barplot(x=
"tip"
, y=
"total_bill"
, data=tips, palette=
'coolwarm'
)
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5bf62888>
python
1
sns.barplot(x=
"tip"
, y=
"total_bill"
, data=tips, palette=
'Reds'
)
<matplotlib.axes._subplots.AxesSubplot at 0x1ba59e40988>
0-4. pandas 데이터프레임과 높은 호환성
python
1
tips
total_bill
tip
sex
smoker
day
time
size
0
16.99
1.01
Female
No
Sun
Dinner
2
1
10.34
1.66
Male
No
Sun
Dinner
3
2
21.01
3.50
Male
No
Sun
Dinner
3
3
23.68
3.31
Male
No
Sun
Dinner
2
4
24.59
3.61
Female
No
Sun
Dinner
4
...
...
...
...
...
...
...
...
239
29.03
5.92
Male
No
Sat
Dinner
3
240
27.18
2.00
Female
Yes
Sat
Dinner
2
241
22.67
2.00
Male
Yes
Sat
Dinner
2
242
17.82
1.75
Male
No
Sat
Dinner
2
243
18.78
3.00
Female
No
Thur
Dinner
2
244 rows × 7 columns
python
1
2
3
4
sns.catplot(x=
"sex"
, y=
"total_bill"
,
data=tips,
kind=
"bar"
)
plt.show()
hue
옵션: bar를 새로운 기준으로 분할
python
1
2
3
4
5
sns.catplot(x=
"sex"
, y=
"total_bill"
,
hue=
"smoker"
,
data=tips,
kind=
"bar"
)
plt.show()
col
/
row
옵션: 그래프 자체를 새로운 기준으로 분할
python
1
2
3
4
5
6
sns.catplot(x=
"sex"
, y=
"total_bill"
,
hue=
"smoker"
,
col=
"time"
,
data=tips,
kind=
"bar"
)
plt.show()
xtick, ytick, xlabel, ylabel을 알아서 생성해 줌
legend까지 자동으로 생성해 줌
뿐만 아니라, 신뢰 구간도 알아서 계산하여 생성함
1. Scatterplot
reference:
<sns.scatterplot> Document
sns.scatterplot
(
x, y, size=None, sizes=None, hue=None, palette=None, color=‘auto’, alpha=‘auto’…
)
sizes
옵션: size의 선택범위를 설정. (사아즈의 min, max를 설정)
hue
옵션: 컬러의 구별 기준이 되는 grouping variable 설정
color
옵션: cmap에 컬러를 지정하면, 컬러 값을 모두 같게 가겨갈 수 있음
alpha
옵션: 투명도 (0~1)
python
1
sns.set_style(
'darkgrid'
)
1-1. x, y, color, area 설정하기
python
1
2
3
4
5
x = np.random.rand(
50
)
y = np.random.rand(
50
)
colors = np.arange(
50
)
area = x * y *
1000
(1) matplotlib
python
1
2
plt.scatter(x, y, s=area, c=colors)
plt.show()
(2) seaborn
python
1
2
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), hue=area, palette=
'coolwarm'
)
plt.show()
[Tip]
Palette 이름이 생각안나면: palette 값을 임의로 주고 실행하여 오류 경고창에 정확한 palette 이름을 보여줌
python
1
2
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), hue=area, palette=
'coolwarm111'
)
plt.show()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
D:\Anaconda\lib\site-packages\seaborn\relational.py in numeric_to_palette(self, data, order, palette, norm)
248 try:
--> 249 cmap = mpl.cm.get_cmap(palette)
250 except (ValueError, TypeError):
D:\Anaconda\lib\site-packages\matplotlib\cm.py in get_cmap(name, lut)
182 "Colormap %s is not recognized. Possible values are: %s"
--> 183 % (name, ', '.join(sorted(cmap_d))))
184
ValueError: Colormap coolwarm111 is not recognized. Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r
1-2. cmap과 alpha
(1) matplotlib
python
1
2
3
4
5
6
7
8
9
10
11
12
13
plt.figure(figsize=(
12
,
6
))
plt.subplot(
131
)
plt.scatter(x, y, s=area, c=
'blue'
, alpha=
0.1
)
plt.title(
'alpha=0.1'
)
plt.subplot(
132
)
plt.title(
'alpha=0.5'
)
plt.scatter(x, y, s=area, c=
'red'
, alpha=
0.5
)
plt.subplot(
133
)
plt.title(
'alpha=1.0'
)
plt.scatter(x, y, s=area, c=
'green'
, alpha=
1.0
)
plt.show()
(2) seaborn
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
plt.figure(figsize=(
12
,
6
))
plt.subplot(
131
)
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), color=
'blue'
, alpha=
0.1
)
plt.title(
'alpha=0.1'
)
plt.subplot(
132
)
plt.title(
'alpha=0.5'
)
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), color=
'red'
, alpha=
0.5
)
plt.subplot(
133
)
plt.title(
'alpha=1.0'
)
sns.scatterplot(x, y, size=area, sizes=(area.min(), area.max()), color=
'green'
, alpha=
0.9
)
plt.show()
2. Barplot, Barhplot
reference:
<sns.barplot> Document
sns.boxplot
(
x, y, hue=None, data=None, alpha=‘auto’, palette=None / color=None
)
2-1. 기본 Barplot 그리기
(1) matplotlib
python
1
2
3
4
5
6
7
8
9
10
11
12
13
x = [
'Math'
,
'Programming'
,
'Data Science'
,
'Art'
,
'English'
,
'Physics'
]
y = [
90
,
60
,
80
,
50
,
70
,
40
]
plt.figure(figsize = (
7
,
4
))
plt.bar(x, y, alpha =
0.7
, color =
'red'
)
plt.title(
'Subjects'
)
plt.xticks(rotation=
20
)
plt.ylabel(
'Grades'
)
plt.show()
(2) seaborn
python
1
2
3
4
5
6
7
8
9
10
11
12
13
x = [
'Math'
,
'Programming'
,
'Data Science'
,
'Art'
,
'English'
,
'Physics'
]
y = [
90
,
60
,
80
,
50
,
70
,
40
]
plt.figure(figsize = (
7
,
4
))
sns.barplot(x, y, alpha=
0.8
, palette=
'YlGnBu'
)
plt.title(
'Subjects'
)
plt.xticks(rotation=
20
)
plt.ylabel(
'Grades'
)
plt.show()
2-2. 기본 Barhplot 그리기
(1) matplotlib
plt.barh
함수 사용
bar 함수에서
xticks / ylabel 로 설정
했던 부분이 barh 함수에서
yticks / xlabel 로 변경함
python
1
2
3
4
5
6
7
8
9
10
11
12
13
x = [
'Math'
,
'Programming'
,
'Data Science'
,
'Art'
,
'English'
,
'Physics'
]
y = [
90
,
60
,
80
,
50
,
70
,
40
]
plt.figure(figsize = (
7
,
5
))
plt.barh(x, y, alpha =
0.7
, color =
'red'
)
plt.title(
'Subjects'
)
plt.yticks(x)
plt.xlabel(
'Grades'
)
plt.show()
(2) seaborn
sns.barplot 함수를 그대로 사용
barplot함수 안에 x와 y의 위치를 교환
xticks설정이 변경 불필요;
하지만 ylabel설정은 xlable로 변경 필요
python
1
2
3
4
5
6
7
8
9
10
11
x = [
'Math'
,
'Programming'
,
'Data Science'
,
'Art'
,
'English'
,
'Physics'
]
y = [
90
,
60
,
80
,
50
,
70
,
40
]
plt.figure(figsize = (
7
,
5
))
sns.barplot(y, x, alpha=
0.9
, palette=
"YlOrRd"
)
plt.xlabel(
'Grades'
)
plt.title(
'Subjects'
)
plt.show()
2-3. Barplot에서 비교 그래프 그리기
(1) matplotlib
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
x_label = [
'Math'
,
'Programming'
,
'Data Science'
,
'Art'
,
'English'
,
'Physics'
]
x = np.arange(len(x_label))
y_1 = [
90
,
60
,
80
,
50
,
70
,
40
]
y_2 = [
80
,
40
,
90
,
60
,
50
,
70
]
width =
0.35
fig, axes = plt.subplots()
axes.bar(x - width/
2
, y_1, width, alpha =
0.5
)
axes.bar(x + width/
2
, y_2, width, alpha =
0.8
)
plt.xticks(x)
axes.set_xticklabels(x_label)
plt.ylabel(
'Grades'
)
plt.title(
'Subjects'
)
plt.legend([
'John'
,
'Peter'
])
plt.show()
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
x_label = [
'Math'
,
'Programming'
,
'Data Science'
,
'Art'
,
'English'
,
'Physics'
]
x = np.arange(len(x_label))
y_1 = [
90
,
60
,
80
,
50
,
70
,
40
]
y_2 = [
80
,
40
,
90
,
60
,
50
,
70
]
width =
0.35
fig, axes = plt.subplots()
axes.barh(x - width/
2
, y_1, width, alpha =
0.5
, color =
"green"
)
axes.barh(x + width/
2
, y_2, width, alpha =
0.5
, color =
"blue"
)
plt.yticks(x)
axes.set_yticklabels(x_label)
plt.xlabel(
'Grades'
)
plt.title(
'Subjects'
)
plt.legend([
'John'
,
'Peter'
])
plt.show()
(2) seaborn
Seaborn에서는 위의
matplotlib
과 조금 다른 방식을 취한다.
seaborn에서
hue
옵션으로 매우 쉽게 비교
barplot
을 그릴 수 있음.
sns.barplot
(
x, y, hue=…, data=…, palette=…
)
실전 tip.
그래프를 임의로 그려야 하는 경우 ->
matplotlib
DataFrame을 가지고 그리는 경우 ->
seaborn
python
1
2
titanic = sns.load_dataset(
'titanic'
)
titanic.head()
survived
pclass
sex
age
sibsp
parch
fare
embarked
class
who
adult_male
deck
embark_town
alive
alone
0
0
3
male
22.0
1
0
7.2500
S
Third
man
True
NaN
Southampton
no
False
1
1
1
female
38.0
1
0
71.2833
C
First
woman
False
C
Cherbourg
yes
False
2
1
3
female
26.0
0
0
7.9250
S
Third
woman
False
NaN
Southampton
yes
True
3
1
1
female
35.0
1
0
53.1000
S
First
woman
False
C
Southampton
yes
False
4
0
3
male
35.0
0
0
8.0500
S
Third
man
True
NaN
Southampton
no
True
python
1
2
sns.barplot(x=
'sex'
, y=
'survived'
, hue=
'pclass'
, data=titanic, palette=
'muted'
)
plt.show()
3. Line Plot
reference:
<sns.lineplot> Document
sns.lineplot
(
x, y, label=…, color=None, alpha=‘auto’, marker=None, linestyle=None
)
기본 옵션은 matplotlib의
plt.plot
과 비슷
함수만
plt.plot
에서
sns.lineplot
로 바꾸면 됨
plt.legend() 명령어 따로 쓸 필요없음
배경이 whitegrid / darkgrid 로 설정되어 있을 시 plt.grid() 명령어 불필요
3-1. 기본 lineplot 그리기
(1) matplotlib
python
1
2
3
4
5
6
7
8
9
10
x = np.arange(
0
,
10
,
0.1
)
y =
1
+ np.sin(x)
plt.plot(x, y)
plt.xlabel(
'x value'
)
plt.ylabel(
'y value'
)
plt.title(
'sin graph'
, fontsize=
16
)
plt.show()
(2) seaborn
python
1
2
3
4
5
6
7
sns.lineplot(x, y)
plt.xlabel(
'x value'
)
plt.ylabel(
'y value'
)
plt.title(
'sin graph'
, fontsize=
16
)
plt.show()
3-2. 2개 이상의 그래프 그리기
python
1
2
3
4
5
6
7
8
9
10
11
12
13
x = np.arange(
0
,
10
,
0.1
)
y_1 =
1
+ np.sin(x)
y_2 =
1
+ np.cos(x)
sns.lineplot(x, y_1,label=
'1+sin'
, color=
'blue'
, alpha =
0.3
)
sns.lineplot(x, y_2, label=
'1+cos'
, color=
'red'
, alpha =
0.7
)
plt.xlabel(
"x value"
)
plt.ylabel(
"y value"
)
plt.title(
"sin and cos graph"
, fontsize =
18
)
plt.show()
3-3. 마커 스타일링
marker: 마커 옵션
python
1
2
3
4
5
6
7
8
9
10
11
12
13
x = np.arange(
0
,
10
,
0.1
)
y_1 =
1
+ np.sin(x)
y_2 =
1
+ np.cos(x)
sns.lineplot(x, y_1, label=
'1+sin'
, color=
'blue'
, alpha=
0.3
, marker=
'o'
)
sns.lineplot(x, y_2, label=
'1+cos'
, color=
'red'
, alpha=
0.7
, marker=
'+'
)
plt.xlabel(
'x value'
)
plt.ylabel(
'y value'
)
plt.title(
'sin and cos graph'
, fontsize =
18
)
plt.show()
3-4. 라인 스타일 변경하기
linestyle: 라인 스타일 변경하기
python
1
2
3
4
5
6
7
8
9
10
11
12
13
x = np.arange(
0
,
10
,
0.1
)
y_1 =
1
+ np.sin(x)
y_2 =
1
+ np.cos(x)
sns.lineplot(x, y_1, label=
'1+sin'
, color=
'blue'
, linestyle=
':'
)
sns.lineplot(x, y_2, label=
'1+cos'
, color=
'red'
, linestyle=
'-.'
)
plt.xlabel(
'x value'
)
plt.ylabel(
'y value'
)
plt.title(
'sin and cos graph'
, fontsize =
18
)
plt.show()
4. Areaplot (Filled Area)
Seaborn에서는
areaplot을 지원하지 않음
matplotlib을 활용하여 구현해야 함
5.Histogram
reference:
<sns.distplot> Document
sns.distplot
(
x, bins=None, hist=True, kde=True, vertical=False
)
bins:
hist bins 갯수 설정
hist:
Whether to plot a (normed) histogram
kde:
Whether to plot a gaussian kernel density estimate
vertical:
If True, observed values are on y-axis
5-1. 기본 Histogram 그리기
(1) matplotlib
python
1
2
3
4
5
6
7
8
N =
100000
bins =
30
x = np.random.randn(N)
plt.hist(x, bins=bins)
plt.show()
(2) seaborn
Histogram + Density Function
(
default
)
python
1
2
3
4
5
6
N =
100000
bins =
30
x = np.random.randn(N)
sns.distplot(x, bins=bins)
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5cc800c8>
Histogram Only
python
1
sns.distplot(x, bins=bins, hist=
True
, kde=
False
, color=
'g'
)
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5cd09788>
Density Function Only
python
1
sns.distplot(x, bins=bins, hist=
False
, kde=
True
, color=
'g'
)
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5c7cc208>
수평 그래프
python
1
sns.distplot(x, bins=bins, vertical=
True
, color=
'r'
)
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5c250108>
5-2. 다중 Histogram 그리기
matplotlib 에서의 방법을 사용
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
N =
100000
bins =
30
x = np.random.randn(N)
fig, axes = plt.subplots(
1
,
3
,
sharey =
True
,
tight_layout =
True
)
fig.set_size_inches(
12
,
5
)
axes[
0
].hist(x, bins = bins)
axes[
1
].hist(x, bins = bins*
2
)
axes[
2
].hist(x, bins = bins*
4
)
plt.show()
6. Pie Chart
Seaborn에서는
pie plot을 지원하지 않음
matplotlib을 활용하여 구현해야 함
7. Box Plot
reference:
<sns.boxplot> Document
sns.baxplot
(
x=None, y=None, hue=None, data=None, orient=None, width=0.8
)
hue:
비교 그래프를 그릴 때 나눔 기준이 되는 Variable 설정
orient:
“v” / “h”. Orientation of the plot (vertical or horizontal)
width:
box의 넓이
7-1. 기본 박스플롯 생성
샘플 데이터 생성
python
1
2
3
4
5
6
spread = np.random.rand(
50
) *
100
center = np.ones(
25
) *
50
flier_high = np.random.rand(
10
) *
100
+
100
flier_low = np.random.rand(
10
) *
-100
data = np.concatenate((spread, center, flier_high, flier_low))
(1) matplotlib
python
1
2
plt.boxplot(data)
plt.show()
(2) seaborn
python
1
2
sns.boxplot(data, orient=
'v'
, width=
0.2
)
plt.show()
7-2. 다중 박스플롯 생성
seaborn에서는
hue
옵션으로 매우 쉽게
비교 boxplot
을 그릴 수 있으며 주로 DataFrame을 가지고 그릴 때 활용한다.
barplot과 마찬가지로, 용도에 따라 적절한 library를 사용한다
실전 Tip.
그래프를 임의로 그려야 하는 경우 ->
matplotlit
DataFrame을 가지고 그리는 경우 ->
seaborn
(1) matplotlib
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
spread1 = np.random.rand(
50
) *
100
center1 = np.ones(
25
) *
50
flier_high1 = np.random.rand(
10
) *
100
+
100
flier_low1 = np.random.rand(
10
) *
-100
data1 = np.concatenate((spread1, center1, flier_high1, flier_low1))
spread2 = np.random.rand(
50
) *
100
center2 = np.ones(
25
) *
40
flier_high2 = np.random.rand(
10
) *
100
+
100
flier_low2 = np.random.rand(
10
) *
-100
data2 = np.concatenate((spread2, center2, flier_high2, flier_low2))
data1.shape = (
-1
,
1
)
data2.shape = (
-1
,
1
)
data = [data1, data2, data2[::
2
,
0
]]
python
1
2
plt.boxplot(data)
plt.show()
(2) seaborn
python
1
2
titanic = sns.load_dataset(
'titanic'
)
titanic.head()
survived
pclass
sex
age
sibsp
parch
fare
embarked
class
who
adult_male
deck
embark_town
alive
alone
0
0
3
male
22.0
1
0
7.2500
S
Third
man
True
NaN
Southampton
no
False
1
1
1
female
38.0
1
0
71.2833
C
First
woman
False
C
Cherbourg
yes
False
2
1
3
female
26.0
0
0
7.9250
S
Third
woman
False
NaN
Southampton
yes
True
3
1
1
female
35.0
1
0
53.1000
S
First
woman
False
C
Southampton
yes
False
4
0
3
male
35.0
0
0
8.0500
S
Third
man
True
NaN
Southampton
no
True
python
1
2
sns.boxplot(x=
'pclass'
, y=
'age'
, hue=
'survived'
, data=titanic)
plt.show()
7-3. Box Plot 축 바꾸기
(1) 단일 boxplot
orient옵션: orient = "h"로 설정
python
1
2
3
4
5
6
spread = np.random.rand(
50
) *
100
center = np.ones(
25
) *
50
flier_high = np.random.rand(
10
) *
100
+
100
flier_low = np.random.rand(
10
) *
-100
data = np.concatenate((spread, center, flier_high, flier_low))
python
1
sns.boxplot(data, orient=
'h'
, width=
0.3
)
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5e866188>
(2) 다중 boxplot
x, y 변수 교환
orient = “h”
python
1
2
sns.boxplot(y=
'pclass'
, x=
'age'
, hue=
'survived'
, data=titanic, orient=
'h'
)
plt.show()
7-4. Outlier 마커 심볼과 컬러 변경
flierprops = …
옵션 사용
(matplotlib과 동일)
python
1
2
3
4
5
6
outlier_marker = dict(markerfacecolor=
'r'
, marker=
'D'
)
plt.title(
'Changed Outlier Symbols'
, fontsize=
15
)
sns.boxplot(data, orient=
'v'
, width=
0.2
, flierprops=outlier_marker)
plt.show() |
| Markdown | Loading...

[Articles102](https://hyemin-kim.github.io/archives/)
[Tags36](https://hyemin-kim.github.io/tags/)
[Categories29](https://hyemin-kim.github.io/categories/)
***
[Home](https://hyemin-kim.github.io/)
[Archives](https://hyemin-kim.github.io/archives/)
[Categories](https://hyemin-kim.github.io/categories/)
[Tags](https://hyemin-kim.github.io/tags/)
[Link](https://hyemin-kim.github.io/link/)
Catalog
You've read0%
1. [Seaborn을 활용한 다양한 그래프 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#seaborn%EC%9D%84-%ED%99%9C%EC%9A%A9%ED%95%9C-%EB%8B%A4%EC%96%91%ED%95%9C-%EA%B7%B8%EB%9E%98%ED%94%84-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
1. [0\. Seaborn 개요](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-seaborn-%EA%B0%9C%EC%9A%94)
1. [0-1. seaborn 에서만 제공되는 통계 기반 plot](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-1-seaborn-%EC%97%90%EC%84%9C%EB%A7%8C-%EC%A0%9C%EA%B3%B5%EB%90%98%EB%8A%94-%ED%86%B5%EA%B3%84-%EA%B8%B0%EB%B0%98-plot)
2. [0-2. 아름다운 스타일링](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-2-%EC%95%84%EB%A6%84%EB%8B%A4%EC%9A%B4-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
3. [0-3. 컬러 팔레트](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-3-%EC%BB%AC%EB%9F%AC-%ED%8C%94%EB%A0%88%ED%8A%B8)
4. [0-4. pandas 데이터프레임과 높은 호환성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-4-pandas-%EB%8D%B0%EC%9D%B4%ED%84%B0%ED%94%84%EB%A0%88%EC%9E%84%EA%B3%BC-%EB%86%92%EC%9D%80-%ED%98%B8%ED%99%98%EC%84%B1)
2. [1\. Scatterplot](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-scatterplot)
1. [1-1. x, y, color, area 설정하기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-1-x-y-color-area-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0)
2. [1-2. cmap과 alpha](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-2-cmap%EA%B3%BC-alpha)
3. [2\. Barplot, Barhplot](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-barplot-barhplot)
1. [2-1. 기본 Barplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-1-%EA%B8%B0%EB%B3%B8-barplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
2. [2-2. 기본 Barhplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-2-%EA%B8%B0%EB%B3%B8-barhplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
3. [2-3. Barplot에서 비교 그래프 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-3-barplot%EC%97%90%EC%84%9C-%EB%B9%84%EA%B5%90-%EA%B7%B8%EB%9E%98%ED%94%84-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
4. [3\. Line Plot](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-line-plot)
1. [3-1. 기본 lineplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-1-%EA%B8%B0%EB%B3%B8-lineplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
2. [3-2. 2개 이상의 그래프 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-2-2%EA%B0%9C-%EC%9D%B4%EC%83%81%EC%9D%98-%EA%B7%B8%EB%9E%98%ED%94%84-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
3. [3-3. 마커 스타일링](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-3-%EB%A7%88%EC%BB%A4-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
4. [3-4. 라인 스타일 변경하기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-4-%EB%9D%BC%EC%9D%B8-%EC%8A%A4%ED%83%80%EC%9D%BC-%EB%B3%80%EA%B2%BD%ED%95%98%EA%B8%B0)
5. [4\. Areaplot (Filled Area)](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#4-areaplot-filled-area)
6. [5\.Histogram](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5histogram)
1. [5-1. 기본 Histogram 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5-1-%EA%B8%B0%EB%B3%B8-histogram-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
2. [5-2. 다중 Histogram 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5-2-%EB%8B%A4%EC%A4%91-histogram-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
7. [6\. Pie Chart](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#6-pie-chart)
8. [7\. Box Plot](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-box-plot)
1. [7-1. 기본 박스플롯 생성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-1-%EA%B8%B0%EB%B3%B8-%EB%B0%95%EC%8A%A4%ED%94%8C%EB%A1%AF-%EC%83%9D%EC%84%B1)
2. [7-2. 다중 박스플롯 생성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-2-%EB%8B%A4%EC%A4%91-%EB%B0%95%EC%8A%A4%ED%94%8C%EB%A1%AF-%EC%83%9D%EC%84%B1)
3. [7-3. Box Plot 축 바꾸기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-3-box-plot-%EC%B6%95-%EB%B0%94%EA%BE%B8%EA%B8%B0)
4. [7-4. Outlier 마커 심볼과 컬러 변경](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-4-outlier-%EB%A7%88%EC%BB%A4-%EC%8B%AC%EB%B3%BC%EA%B3%BC-%EC%BB%AC%EB%9F%AC-%EB%B3%80%EA%B2%BD)
[Hyemin Kim](https://hyemin-kim.github.io/)
[Search]()
[Home](https://hyemin-kim.github.io/)
[Archives](https://hyemin-kim.github.io/archives/)
[Categories](https://hyemin-kim.github.io/categories/)
[Tags](https://hyemin-kim.github.io/tags/)
[Link](https://hyemin-kim.github.io/link/)
Python \>\> Seaborn - (1) Seaborn을 활용한 다양한 그래프 그리기
Created
2020-07-03
\|Updated
2020-11-06
\|[【STUDY - Python】](https://hyemin-kim.github.io/categories/%E3%80%90STUDY-Python%E3%80%91/)[Python - 4. Seaborn](https://hyemin-kim.github.io/categories/%E3%80%90STUDY-Python%E3%80%91/Python-4-Seaborn/)[Python - 시각화](https://hyemin-kim.github.io/categories/%E3%80%90STUDY-Python%E3%80%91/Python-%EC%8B%9C%EA%B0%81%ED%99%94/)
\|Post View:
# Seaborn을 활용한 다양한 그래프 그리기
- [**0\. Seaborn 개요**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-seaborn-%EA%B0%9C%EC%9A%94)
- [0-1. seaborn 에서만 제공되는 통계 기반 plot](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-1-seaborn-%EC%97%90%EC%84%9C%EB%A7%8C-%EC%A0%9C%EA%B3%B5%EB%90%98%EB%8A%94-%ED%86%B5%EA%B3%84-%EA%B8%B0%EB%B0%98-plot)
- [0-2. 아름다운 스타일링](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-2-%EC%95%84%EB%A6%84%EB%8B%A4%EC%9A%B4-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
- [0-3. 컬러 팔레트](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-3-%EC%BB%AC%EB%9F%AC-%ED%8C%94%EB%A0%88%ED%8A%B8)
- [0-4. pandas 데이터프레임과 높은 호환성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-4-pandas-%EB%8D%B0%EC%9D%B4%ED%84%B0%ED%94%84%EB%A0%88%EC%9E%84%EA%B3%BC-%EB%86%92%EC%9D%80-%ED%98%B8%ED%99%98%EC%84%B1)
- [**1\. Scatterplot**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-scatterplot)
- [1-1. x, y, color, area 설정하기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-1-x-y-color-area-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0)
- [1-2. cmap과 alpha](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-2-cmap%EA%B3%BC-alpha)
- [**2\. Barplot, Barhplot**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-barplot-barhplot)
- [2-1. 기본 Barplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-1-%EA%B8%B0%EB%B3%B8-barplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [2-2. 기본 Barhplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-2-%EA%B8%B0%EB%B3%B8-barhplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [2-3. Barplot에서 비교 그래프 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-3-barplot%EC%97%90%EC%84%9C-%EB%B9%84%EA%B5%90-%EA%B7%B8%EB%9E%98%ED%94%84-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [**3\. Line Plot**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-line-plot)
- [3-1. 기본 lineplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-1-%EA%B8%B0%EB%B3%B8-lineplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [3-2. 2개 이상의 그래프 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-2-2%EA%B0%9C-%EC%9D%B4%EC%83%81%EC%9D%98-%EA%B7%B8%EB%9E%98%ED%94%84-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [3-3. 마커 스타일링](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-3-%EB%A7%88%EC%BB%A4-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
- [3-4. 라인 스타일 변경하기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-4-%EB%9D%BC%EC%9D%B8-%EC%8A%A4%ED%83%80%EC%9D%BC-%EB%B3%80%EA%B2%BD%ED%95%98%EA%B8%B0)
- [**4\. Areaplot (Filled Area)**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#4-areaplot-filled-area)
- [**5\.Histogram**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5histogram)
- [5-1. 기본 Histogram 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5-1-%EA%B8%B0%EB%B3%B8-histogram-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [5-2. 다중 Histogram 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5-2-%EB%8B%A4%EC%A4%91-histogram-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [**6\. Pie Chart**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#6-pie-chart)
- [**7\. Box Plot**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-box-plot)
- [7-1. 기본 박스플롯 생성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-1-%EA%B8%B0%EB%B3%B8-%EB%B0%95%EC%8A%A4%ED%94%8C%EB%A1%AF-%EC%83%9D%EC%84%B1)
- [7-2. 다중 박스플롯 생성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-2-%EB%8B%A4%EC%A4%91-%EB%B0%95%EC%8A%A4%ED%94%8C%EB%A1%AF-%EC%83%9D%EC%84%B1)
- [7-3. Box Plot 축 바꾸기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-3-box-plot-%EC%B6%95-%EB%B0%94%EA%BE%B8%EA%B8%B0)
- [7-4. Outlier 마커 심볼과 컬러 변경](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-4-outlier-%EB%A7%88%EC%BB%A4-%EC%8B%AC%EB%B3%BC%EA%B3%BC-%EC%BB%AC%EB%9F%AC-%EB%B3%80%EA%B2%BD)
> ***reference:***
>
> - [pyplot 공식 도튜먼트 살펴보기](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot)
> - [seaborn 공식 도큐먼트 살펴보기](https://seaborn.pydata.org/)
python
python
## **0\. Seaborn 개요**
seaborn은 matplotlib을 더 사용하게 쉽게 해주는 라이브러리다.
matplotlib으로 대부분의 시각화는 가능하지만, 다음과 같은 이유로 많은 사람들이 `seaborn`을 선호한다.
> **비교:** [matplotlib을 활용한 다양한 그래프 그리기](https://hyemin-kim.github.io/2020/06/28/S-Python-Matplotlib2/)
### 0-1. seaborn 에서만 제공되는 통계 기반 plot
python
**(1) violinplot**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_14_0.png)
**(2) countplot**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_17_0.png)
**(3) relplot**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_20_0.png)
**(4) lmplot**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_23_0.png)
**(5) heatmap**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_26_0.png)
### 0-2. 아름다운 스타일링
**(1) default color의 예쁜 조합**
seaborn의 최대 장점 중의 하나가 아름다운 컬러팔레트다.
스타일링에 크게 신경 쓰지 않아도 default 컬러가 예쁘게 조합해준다.
**matplotlib VS seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_32_0.png)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_33_0.png)
**(2) 그래프 배경 설정**
그래프의 배경 (grid 스타일)을 설정할 수 있음.
> **sns.set\_style(’…’)**
>
> - whitegrid: white background + grid
> - darkgrid: dark background + grid
> - white: white background (without grid)
> - dark: dark background (without grid)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_38_0.png)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_39_0.png)
### 0-3. 컬러 팔레트
자세한 컬러팔레트는 [공식 도큐먼트](https://chrisalbon.com/python/data_visualization/seaborn_color_palettes/)를 참고
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_0.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_1.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_2.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_3.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_4.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_5.png)
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5bf62888>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_44_1.png)
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba59e40988>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_45_1.png)
### 0-4. pandas 데이터프레임과 높은 호환성
python
| | total\_bill | tip | sex | smoker | day | time | size |
|---|---|---|---|---|---|---|---|
| 0 | 16\.99 | 1\.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10\.34 | 1\.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21\.01 | 3\.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23\.68 | 3\.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24\.59 | 3\.61 | Female | No | Sun | Dinner | 4 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 239 | 29\.03 | 5\.92 | Male | No | Sat | Dinner | 3 |
| 240 | 27\.18 | 2\.00 | Female | Yes | Sat | Dinner | 2 |
| 241 | 22\.67 | 2\.00 | Male | Yes | Sat | Dinner | 2 |
| 242 | 17\.82 | 1\.75 | Male | No | Sat | Dinner | 2 |
| 243 | 18\.78 | 3\.00 | Female | No | Thur | Dinner | 2 |
244 rows × 7 columns
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_50_0.png)
- `hue`옵션: bar를 새로운 기준으로 분할
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_53_0.png)
- `col` / `row` 옵션: 그래프 자체를 새로운 기준으로 분할
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_56_0.png)
- xtick, ytick, xlabel, ylabel을 알아서 생성해 줌
- legend까지 자동으로 생성해 줌
- 뿐만 아니라, 신뢰 구간도 알아서 계산하여 생성함
## **1\. Scatterplot**
> ***reference:*** [\<sns.scatterplot\> Document](https://seaborn.pydata.org/generated/seaborn.scatterplot.html)
> **sns.scatterplot** ( *x, y, size=None, sizes=None, hue=None, palette=None, color=‘auto’, alpha=‘auto’…* )
>
> - `sizes` 옵션: size의 선택범위를 설정. (사아즈의 min, max를 설정)
> - `hue` 옵션: 컬러의 구별 기준이 되는 grouping variable 설정
> - `color` 옵션: cmap에 컬러를 지정하면, 컬러 값을 모두 같게 가겨갈 수 있음
> - `alpha` 옵션: 투명도 (0~1)
python
### 1-1. x, y, color, area 설정하기
python
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_69_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_72_0.png)
**\[Tip\]** Palette 이름이 생각안나면: palette 값을 임의로 주고 실행하여 오류 경고창에 정확한 palette 이름을 보여줌
python
```
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
D:\Anaconda\lib\site-packages\seaborn\relational.py in numeric_to_palette(self, data, order, palette, norm)
248 try:
--> 249 cmap = mpl.cm.get_cmap(palette)
250 except (ValueError, TypeError):
D:\Anaconda\lib\site-packages\matplotlib\cm.py in get_cmap(name, lut)
182 "Colormap %s is not recognized. Possible values are: %s"
--> 183 % (name, ', '.join(sorted(cmap_d))))
184
ValueError: Colormap coolwarm111 is not recognized. Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r
```
### 1-2. cmap과 alpha
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_79_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_82_0.png)
## **2\. Barplot, Barhplot**
> ***reference:*** [\<sns.barplot\> Document](https://seaborn.pydata.org/generated/seaborn.barplot.html)
> **sns.boxplot** ( *x, y, hue=None, data=None, alpha=‘auto’, palette=None / color=None* )
### 2-1. 기본 Barplot 그리기
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_91_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_94_0.png)
### 2-2. 기본 Barhplot 그리기
**(1) matplotlib**
> - **plt.barh** 함수 사용
> - bar 함수에서 **xticks / ylabel 로 설정**했던 부분이 barh 함수에서 **yticks / xlabel 로 변경함**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_99_0.png)
**(2) seaborn**
> - sns.barplot 함수를 그대로 사용
> - barplot함수 안에 x와 y의 위치를 교환
> xticks설정이 변경 불필요;
> 하지만 ylabel설정은 xlable로 변경 필요
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_102_0.png)
### 2-3. Barplot에서 비교 그래프 그리기
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_106_0.png)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_107_0.png)
**(2) seaborn**
Seaborn에서는 위의 `matplotlib`과 조금 다른 방식을 취한다.
seaborn에서 `hue`옵션으로 매우 쉽게 비교 **barplot**을 그릴 수 있음.
> **sns.barplot** ( *x, y, hue=…, data=…, palette=…* )
**실전 tip.**
- 그래프를 임의로 그려야 하는 경우 -\> `matplotlib`
- DataFrame을 가지고 그리는 경우 -\> `seaborn`
python
| | survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult\_male | deck | embark\_town | alive | alone |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22\.0 | 1 | 0 | 7\.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38\.0 | 1 | 0 | 71\.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26\.0 | 0 | 0 | 7\.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35\.0 | 1 | 0 | 53\.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35\.0 | 0 | 0 | 8\.0500 | S | Third | man | True | NaN | Southampton | no | True |
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_115_0.png)
## **3\. Line Plot**
> ***reference:*** [\<sns.lineplot\> Document](https://seaborn.pydata.org/generated/seaborn.lineplot.html)
> **sns.lineplot** ( *x, y, label=…, color=None, alpha=‘auto’, marker=None, linestyle=None* )
>
> - 기본 옵션은 matplotlib의 `plt.plot`과 비슷
> - 함수만 `plt.plot`에서 `sns.lineplot`로 바꾸면 됨
> - plt.legend() 명령어 따로 쓸 필요없음
> - 배경이 whitegrid / darkgrid 로 설정되어 있을 시 plt.grid() 명령어 불필요
### 3-1. 기본 lineplot 그리기
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_124_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_127_0.png)
### 3-2. 2개 이상의 그래프 그리기
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_130_0.png)
### 3-3. 마커 스타일링
- marker: 마커 옵션
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_134_0.png)
### 3-4. 라인 스타일 변경하기
- linestyle: 라인 스타일 변경하기
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_138_0.png)
## **4\. Areaplot (Filled Area)**
> Seaborn에서는 **areaplot을 지원하지 않음**
> matplotlib을 활용하여 구현해야 함
## **5\.Histogram**
> ***reference:*** [\<sns.distplot\> Document](https://seaborn.pydata.org/generated/seaborn.distplot.html)
> **sns.distplot** ( *x, bins=None, hist=True, kde=True, vertical=False* )
>
> - **bins:** hist bins 갯수 설정
> - **hist:** Whether to plot a (normed) histogram
> - **kde:** Whether to plot a gaussian kernel density estimate
> - **vertical:** If True, observed values are on y-axis
### 5-1. 기본 Histogram 그리기
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_150_0.png)
**(2) seaborn**
**Histogram + Density Function** (*default*)
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5cc800c8>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_154_1.png)
**Histogram Only**
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5cd09788>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_157_1.png)
**Density Function Only**
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5c7cc208>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_160_1.png)
**수평 그래프**
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5c250108>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_163_1.png)
### 5-2. 다중 Histogram 그리기
matplotlib 에서의 방법을 사용
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_167_0.png)
## **6\. Pie Chart**
> Seaborn에서는 **pie plot을 지원하지 않음**
> matplotlib을 활용하여 구현해야 함
## **7\. Box Plot**
> ***reference:*** [\<sns.boxplot\> Document](https://seaborn.pydata.org/generated/seaborn.boxplot.html)
> **sns.baxplot** ( *x=None, y=None, hue=None, data=None, orient=None, width=0.8* )
>
> - **hue:** 비교 그래프를 그릴 때 나눔 기준이 되는 Variable 설정
> - **orient:** “v” / “h”. Orientation of the plot (vertical or horizontal)
> - **width:** box의 넓이
### 7-1. 기본 박스플롯 생성
**샘플 데이터 생성**
python
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_182_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_185_0.png)
### 7-2. 다중 박스플롯 생성
seaborn에서는 `hue`옵션으로 매우 쉽게 **비교 boxplot**을 그릴 수 있으며 주로 DataFrame을 가지고 그릴 때 활용한다.
barplot과 마찬가지로, 용도에 따라 적절한 library를 사용한다
**실전 Tip.**
- 그래프를 임의로 그려야 하는 경우 -\> `matplotlit`
- DataFrame을 가지고 그리는 경우 -\> `seaborn`
**(1) matplotlib**
python
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_194_0.png)
**(2) seaborn**
python
| | survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult\_male | deck | embark\_town | alive | alone |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22\.0 | 1 | 0 | 7\.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38\.0 | 1 | 0 | 71\.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26\.0 | 0 | 0 | 7\.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35\.0 | 1 | 0 | 53\.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35\.0 | 0 | 0 | 8\.0500 | S | Third | man | True | NaN | Southampton | no | True |
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_199_0.png)
### 7-3. Box Plot 축 바꾸기
**(1) 단일 boxplot**
- orient옵션: orient = "h"로 설정
python
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5e866188>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_205_1.png)
**(2) 다중 boxplot**
1. x, y 변수 교환
2. orient = “h”
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_209_0.png)
### 7-4. Outlier 마커 심볼과 컬러 변경
- **flierprops = …** 옵션 사용
(matplotlib과 동일)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_213_0.png)
Author: [Hyemin Kim](mailto:undefined)
Link: <https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/>
Copyright Notice: All articles in this blog are licensed under [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/) unless stating additionally.
[Python](https://hyemin-kim.github.io/tags/Python/)[시각화](https://hyemin-kim.github.io/tags/%EC%8B%9C%EA%B0%81%ED%99%94/)[Seaborn](https://hyemin-kim.github.io/tags/Seaborn/)
[微信扫一扫:分享  微信里点“发现”,扫一下 二维码便可将本文分享至朋友圈。]()
[Previous Post Python \>\> Seaborn - (2) 통계 기반의 시각화](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn2/)
[Next Post Python \>\> Matplotlib - (2) 다양한 그래프 그리기](https://hyemin-kim.github.io/2020/06/28/S-Python-Matplotlib2/)
Related Articles
[2020-07-03 Python \>\> Seaborn - (2) 통계 기반의 시각화](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn2/ "Python >> Seaborn - (2) 통계 기반의 시각화")
[2020-06-28 Python \>\> Matplotlib - (1) 기본 canvas 그리기 및 스타일링](https://hyemin-kim.github.io/2020/06/28/S-Python-Matplotlib1/ "Python >> Matplotlib - (1) 기본 canvas 그리기 및 스타일링")
[2020-06-28 Python \>\> Matplotlib - (2) 다양한 그래프 그리기](https://hyemin-kim.github.io/2020/06/28/S-Python-Matplotlib2/ "Python >> Matplotlib - (2) 다양한 그래프 그리기")
[2020-06-25 Python \>\> Pandas 시각화](https://hyemin-kim.github.io/2020/06/25/S-Python-Pandas-visual/ "Python >> Pandas 시각화")
***
Comment
Related [Issues](https://github.com/hyemin-Kim/hyemin-Kim.github.io/issues) not found
Please contact @ to initialize the comment
Login with GitHub
©2020 - 2023 By Hyemin Kim
Framework [Hexo](https://hexo.io/)\|Theme [Butterfly](https://github.com/jerryc127/hexo-theme-butterfly)
Local search
***
Powered by [hexo-generator-search](https://github.com/wzpan/hexo-generator-search) |
| Readable Markdown | - [**0\. Seaborn 개요**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-seaborn-%EA%B0%9C%EC%9A%94)
- [0-1. seaborn 에서만 제공되는 통계 기반 plot](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-1-seaborn-%EC%97%90%EC%84%9C%EB%A7%8C-%EC%A0%9C%EA%B3%B5%EB%90%98%EB%8A%94-%ED%86%B5%EA%B3%84-%EA%B8%B0%EB%B0%98-plot)
- [0-2. 아름다운 스타일링](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-2-%EC%95%84%EB%A6%84%EB%8B%A4%EC%9A%B4-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
- [0-3. 컬러 팔레트](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-3-%EC%BB%AC%EB%9F%AC-%ED%8C%94%EB%A0%88%ED%8A%B8)
- [0-4. pandas 데이터프레임과 높은 호환성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#0-4-pandas-%EB%8D%B0%EC%9D%B4%ED%84%B0%ED%94%84%EB%A0%88%EC%9E%84%EA%B3%BC-%EB%86%92%EC%9D%80-%ED%98%B8%ED%99%98%EC%84%B1)
- [**1\. Scatterplot**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-scatterplot)
- [1-1. x, y, color, area 설정하기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-1-x-y-color-area-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0)
- [1-2. cmap과 alpha](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#1-2-cmap%EA%B3%BC-alpha)
- [**2\. Barplot, Barhplot**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-barplot-barhplot)
- [2-1. 기본 Barplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-1-%EA%B8%B0%EB%B3%B8-barplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [2-2. 기본 Barhplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-2-%EA%B8%B0%EB%B3%B8-barhplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [2-3. Barplot에서 비교 그래프 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#2-3-barplot%EC%97%90%EC%84%9C-%EB%B9%84%EA%B5%90-%EA%B7%B8%EB%9E%98%ED%94%84-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [**3\. Line Plot**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-line-plot)
- [3-1. 기본 lineplot 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-1-%EA%B8%B0%EB%B3%B8-lineplot-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [3-2. 2개 이상의 그래프 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-2-2%EA%B0%9C-%EC%9D%B4%EC%83%81%EC%9D%98-%EA%B7%B8%EB%9E%98%ED%94%84-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [3-3. 마커 스타일링](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-3-%EB%A7%88%EC%BB%A4-%EC%8A%A4%ED%83%80%EC%9D%BC%EB%A7%81)
- [3-4. 라인 스타일 변경하기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#3-4-%EB%9D%BC%EC%9D%B8-%EC%8A%A4%ED%83%80%EC%9D%BC-%EB%B3%80%EA%B2%BD%ED%95%98%EA%B8%B0)
- [**4\. Areaplot (Filled Area)**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#4-areaplot-filled-area)
- [**5\.Histogram**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5histogram)
- [5-1. 기본 Histogram 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5-1-%EA%B8%B0%EB%B3%B8-histogram-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [5-2. 다중 Histogram 그리기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#5-2-%EB%8B%A4%EC%A4%91-histogram-%EA%B7%B8%EB%A6%AC%EA%B8%B0)
- [**6\. Pie Chart**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#6-pie-chart)
- [**7\. Box Plot**](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-box-plot)
- [7-1. 기본 박스플롯 생성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-1-%EA%B8%B0%EB%B3%B8-%EB%B0%95%EC%8A%A4%ED%94%8C%EB%A1%AF-%EC%83%9D%EC%84%B1)
- [7-2. 다중 박스플롯 생성](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-2-%EB%8B%A4%EC%A4%91-%EB%B0%95%EC%8A%A4%ED%94%8C%EB%A1%AF-%EC%83%9D%EC%84%B1)
- [7-3. Box Plot 축 바꾸기](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-3-box-plot-%EC%B6%95-%EB%B0%94%EA%BE%B8%EA%B8%B0)
- [7-4. Outlier 마커 심볼과 컬러 변경](https://hyemin-kim.github.io/2020/07/03/S-Python-Seaborn1/#7-4-outlier-%EB%A7%88%EC%BB%A4-%EC%8B%AC%EB%B3%BC%EA%B3%BC-%EC%BB%AC%EB%9F%AC-%EB%B3%80%EA%B2%BD)
> ***reference:***
>
> - [pyplot 공식 도튜먼트 살펴보기](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot)
> - [seaborn 공식 도큐먼트 살펴보기](https://seaborn.pydata.org/)
python
python
## **0\. Seaborn 개요**
seaborn은 matplotlib을 더 사용하게 쉽게 해주는 라이브러리다.
matplotlib으로 대부분의 시각화는 가능하지만, 다음과 같은 이유로 많은 사람들이 `seaborn`을 선호한다.
> **비교:** [matplotlib을 활용한 다양한 그래프 그리기](https://hyemin-kim.github.io/2020/06/28/S-Python-Matplotlib2/)
### 0-1. seaborn 에서만 제공되는 통계 기반 plot
python
**(1) violinplot**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_14_0.png)
**(2) countplot**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_17_0.png)
**(3) relplot**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_20_0.png)
**(4) lmplot**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_23_0.png)
**(5) heatmap**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_26_0.png)
### 0-2. 아름다운 스타일링
**(1) default color의 예쁜 조합**
seaborn의 최대 장점 중의 하나가 아름다운 컬러팔레트다.
스타일링에 크게 신경 쓰지 않아도 default 컬러가 예쁘게 조합해준다.
**matplotlib VS seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_32_0.png)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_33_0.png)
**(2) 그래프 배경 설정**
그래프의 배경 (grid 스타일)을 설정할 수 있음.
> **sns.set\_style(’…’)**
>
> - whitegrid: white background + grid
> - darkgrid: dark background + grid
> - white: white background (without grid)
> - dark: dark background (without grid)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_38_0.png)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_39_0.png)
### 0-3. 컬러 팔레트
자세한 컬러팔레트는 [공식 도큐먼트](https://chrisalbon.com/python/data_visualization/seaborn_color_palettes/)를 참고
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_0.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_1.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_2.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_3.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_4.png)
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_43_5.png)
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5bf62888>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_44_1.png)
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba59e40988>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_45_1.png)
### 0-4. pandas 데이터프레임과 높은 호환성
python
| | total\_bill | tip | sex | smoker | day | time | size |
|---|---|---|---|---|---|---|---|
| 0 | 16\.99 | 1\.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10\.34 | 1\.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21\.01 | 3\.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23\.68 | 3\.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24\.59 | 3\.61 | Female | No | Sun | Dinner | 4 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 239 | 29\.03 | 5\.92 | Male | No | Sat | Dinner | 3 |
| 240 | 27\.18 | 2\.00 | Female | Yes | Sat | Dinner | 2 |
| 241 | 22\.67 | 2\.00 | Male | Yes | Sat | Dinner | 2 |
| 242 | 17\.82 | 1\.75 | Male | No | Sat | Dinner | 2 |
| 243 | 18\.78 | 3\.00 | Female | No | Thur | Dinner | 2 |
244 rows × 7 columns
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_50_0.png)
- `hue`옵션: bar를 새로운 기준으로 분할
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_53_0.png)
- `col` / `row` 옵션: 그래프 자체를 새로운 기준으로 분할
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_56_0.png)
- xtick, ytick, xlabel, ylabel을 알아서 생성해 줌
- legend까지 자동으로 생성해 줌
- 뿐만 아니라, 신뢰 구간도 알아서 계산하여 생성함
## **1\. Scatterplot**
> ***reference:*** [\<sns.scatterplot\> Document](https://seaborn.pydata.org/generated/seaborn.scatterplot.html)
> **sns.scatterplot** ( *x, y, size=None, sizes=None, hue=None, palette=None, color=‘auto’, alpha=‘auto’…* )
>
> - `sizes` 옵션: size의 선택범위를 설정. (사아즈의 min, max를 설정)
> - `hue` 옵션: 컬러의 구별 기준이 되는 grouping variable 설정
> - `color` 옵션: cmap에 컬러를 지정하면, 컬러 값을 모두 같게 가겨갈 수 있음
> - `alpha` 옵션: 투명도 (0~1)
python
### 1-1. x, y, color, area 설정하기
python
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_69_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_72_0.png)
**\[Tip\]** Palette 이름이 생각안나면: palette 값을 임의로 주고 실행하여 오류 경고창에 정확한 palette 이름을 보여줌
python
```
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
D:\Anaconda\lib\site-packages\seaborn\relational.py in numeric_to_palette(self, data, order, palette, norm)
248 try:
--> 249 cmap = mpl.cm.get_cmap(palette)
250 except (ValueError, TypeError):
D:\Anaconda\lib\site-packages\matplotlib\cm.py in get_cmap(name, lut)
182 "Colormap %s is not recognized. Possible values are: %s"
--> 183 % (name, ', '.join(sorted(cmap_d))))
184
ValueError: Colormap coolwarm111 is not recognized. Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r
```
### 1-2. cmap과 alpha
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_79_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_82_0.png)
## **2\. Barplot, Barhplot**
> ***reference:*** [\<sns.barplot\> Document](https://seaborn.pydata.org/generated/seaborn.barplot.html)
> **sns.boxplot** ( *x, y, hue=None, data=None, alpha=‘auto’, palette=None / color=None* )
### 2-1. 기본 Barplot 그리기
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_91_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_94_0.png)
### 2-2. 기본 Barhplot 그리기
**(1) matplotlib**
> - **plt.barh** 함수 사용
> - bar 함수에서 **xticks / ylabel 로 설정**했던 부분이 barh 함수에서 **yticks / xlabel 로 변경함**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_99_0.png)
**(2) seaborn**
> - sns.barplot 함수를 그대로 사용
> - barplot함수 안에 x와 y의 위치를 교환
> xticks설정이 변경 불필요;
> 하지만 ylabel설정은 xlable로 변경 필요
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_102_0.png)
### 2-3. Barplot에서 비교 그래프 그리기
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_106_0.png)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_107_0.png)
**(2) seaborn**
Seaborn에서는 위의 `matplotlib`과 조금 다른 방식을 취한다.
seaborn에서 `hue`옵션으로 매우 쉽게 비교 **barplot**을 그릴 수 있음.
> **sns.barplot** ( *x, y, hue=…, data=…, palette=…* )
**실전 tip.**
- 그래프를 임의로 그려야 하는 경우 -\> `matplotlib`
- DataFrame을 가지고 그리는 경우 -\> `seaborn`
python
| | survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult\_male | deck | embark\_town | alive | alone |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22\.0 | 1 | 0 | 7\.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38\.0 | 1 | 0 | 71\.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26\.0 | 0 | 0 | 7\.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35\.0 | 1 | 0 | 53\.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35\.0 | 0 | 0 | 8\.0500 | S | Third | man | True | NaN | Southampton | no | True |
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_115_0.png)
## **3\. Line Plot**
> ***reference:*** [\<sns.lineplot\> Document](https://seaborn.pydata.org/generated/seaborn.lineplot.html)
> **sns.lineplot** ( *x, y, label=…, color=None, alpha=‘auto’, marker=None, linestyle=None* )
>
> - 기본 옵션은 matplotlib의 `plt.plot`과 비슷
> - 함수만 `plt.plot`에서 `sns.lineplot`로 바꾸면 됨
> - plt.legend() 명령어 따로 쓸 필요없음
> - 배경이 whitegrid / darkgrid 로 설정되어 있을 시 plt.grid() 명령어 불필요
### 3-1. 기본 lineplot 그리기
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_124_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_127_0.png)
### 3-2. 2개 이상의 그래프 그리기
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_130_0.png)
### 3-3. 마커 스타일링
- marker: 마커 옵션
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_134_0.png)
### 3-4. 라인 스타일 변경하기
- linestyle: 라인 스타일 변경하기
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_138_0.png)
## **4\. Areaplot (Filled Area)**
> Seaborn에서는 **areaplot을 지원하지 않음**
> matplotlib을 활용하여 구현해야 함
## **5\.Histogram**
> ***reference:*** [\<sns.distplot\> Document](https://seaborn.pydata.org/generated/seaborn.distplot.html)
> **sns.distplot** ( *x, bins=None, hist=True, kde=True, vertical=False* )
>
> - **bins:** hist bins 갯수 설정
> - **hist:** Whether to plot a (normed) histogram
> - **kde:** Whether to plot a gaussian kernel density estimate
> - **vertical:** If True, observed values are on y-axis
### 5-1. 기본 Histogram 그리기
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_150_0.png)
**(2) seaborn**
**Histogram + Density Function** (*default*)
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5cc800c8>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_154_1.png)
**Histogram Only**
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5cd09788>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_157_1.png)
**Density Function Only**
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5c7cc208>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_160_1.png)
**수평 그래프**
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5c250108>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_163_1.png)
### 5-2. 다중 Histogram 그리기
matplotlib 에서의 방법을 사용
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_167_0.png)
## **6\. Pie Chart**
> Seaborn에서는 **pie plot을 지원하지 않음**
> matplotlib을 활용하여 구현해야 함
## **7\. Box Plot**
> ***reference:*** [\<sns.boxplot\> Document](https://seaborn.pydata.org/generated/seaborn.boxplot.html)
> **sns.baxplot** ( *x=None, y=None, hue=None, data=None, orient=None, width=0.8* )
>
> - **hue:** 비교 그래프를 그릴 때 나눔 기준이 되는 Variable 설정
> - **orient:** “v” / “h”. Orientation of the plot (vertical or horizontal)
> - **width:** box의 넓이
### 7-1. 기본 박스플롯 생성
**샘플 데이터 생성**
python
**(1) matplotlib**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_182_0.png)
**(2) seaborn**
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_185_0.png)
### 7-2. 다중 박스플롯 생성
seaborn에서는 `hue`옵션으로 매우 쉽게 **비교 boxplot**을 그릴 수 있으며 주로 DataFrame을 가지고 그릴 때 활용한다.
barplot과 마찬가지로, 용도에 따라 적절한 library를 사용한다
**실전 Tip.**
- 그래프를 임의로 그려야 하는 경우 -\> `matplotlit`
- DataFrame을 가지고 그리는 경우 -\> `seaborn`
**(1) matplotlib**
python
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_194_0.png)
**(2) seaborn**
python
| | survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult\_male | deck | embark\_town | alive | alone |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22\.0 | 1 | 0 | 7\.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38\.0 | 1 | 0 | 71\.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26\.0 | 0 | 0 | 7\.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35\.0 | 1 | 0 | 53\.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35\.0 | 0 | 0 | 8\.0500 | S | Third | man | True | NaN | Southampton | no | True |
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_199_0.png)
### 7-3. Box Plot 축 바꾸기
**(1) 단일 boxplot**
- orient옵션: orient = "h"로 설정
python
python
```
<matplotlib.axes._subplots.AxesSubplot at 0x1ba5e866188>
```
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_205_1.png)
**(2) 다중 boxplot**
1. x, y 변수 교환
2. orient = “h”
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_209_0.png)
### 7-4. Outlier 마커 심볼과 컬러 변경
- **flierprops = …** 옵션 사용 (matplotlib과 동일)
python
[](https://hyemin-kim.github.io/images/S-Python-Seaborn1/output_213_0.png) |
| Shard | 143 (laksa) |
| Root Hash | 2566890010099092343 |
| Unparsed URL | io,github!hyemin-kim,/2020/07/03/S-Python-Seaborn1/ s443 |