Seaborn
Seaborn
통계적 데이터 시각화. Matplotlib 기반, 고수준 API.
Seaborn
통계적 데이터 시각화. Matplotlib 기반, 고수준 API.
Seaborn은 2012년 Michael Waskom이 개발한 Python 통계 시각화 라이브러리입니다. Matplotlib 위에 구축되어 있으며, 더 간결한 API와 미려한 기본 스타일로 통계적 데이터 탐색을 쉽게 합니다. Pandas DataFrame과 긴밀하게 통합됩니다.
통계적 시각화에 특화되어 있습니다. 분포 시각화(histplot, kdeplot, boxplot, violinplot), 범주형 데이터(catplot, barplot, countplot), 회귀 분석(regplot, lmplot), 상관관계(heatmap, pairplot)를 한 줄 코드로 생성합니다.
FacetGrid와 PairGrid로 다변량 분석을 수행합니다. 데이터를 행/열로 분할해 여러 서브플롯을 생성하고, hue 파라미터로 추가 변수를 색상으로 인코딩합니다. relplot, catplot, displot 등 Figure-level 함수로 복잡한 시각화를 간단히 구성합니다.
테마(set_theme), 색상 팔레트(color_palette), 컨텍스트(set_context)로 시각화 스타일을 일괄 설정합니다. 기본 스타일이 학술 논문 수준으로 깔끔해 별도 커스터마이징 없이도 발표용으로 사용 가능합니다.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# 테마 설정
sns.set_theme(style="whitegrid", palette="husl", font_scale=1.1)
# 샘플 데이터
tips = sns.load_dataset("tips")
iris = sns.load_dataset("iris")
# 1. 분포 시각화
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
# 히스토그램 + KDE
sns.histplot(tips['total_bill'], kde=True, ax=axes[0, 0], color='steelblue')
axes[0, 0].set_title('Total Bill Distribution')
# 박스플롯
sns.boxplot(x='day', y='total_bill', hue='sex', data=tips, ax=axes[0, 1])
axes[0, 1].set_title('Total Bill by Day and Sex')
# 바이올린 플롯
sns.violinplot(x='day', y='tip', data=tips, ax=axes[1, 0], inner='box')
axes[1, 0].set_title('Tip Distribution by Day')
# KDE 플롯 (2D)
sns.kdeplot(x=tips['total_bill'], y=tips['tip'], ax=axes[1, 1], fill=True)
axes[1, 1].set_title('2D Density Plot')
plt.tight_layout()
plt.show()
# 2. 회귀 분석 시각화
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# 회귀선이 포함된 산점도
sns.regplot(x='total_bill', y='tip', data=tips, ax=axes[0],
scatter_kws={'alpha': 0.5}, line_kws={'color': 'red'})
axes[0].set_title('Tip vs Total Bill with Regression Line')
# 카테고리별 회귀선 (lmplot - Figure-level)
# sns.lmplot(x='total_bill', y='tip', hue='smoker', col='time', data=tips)
# 잔차 플롯
sns.residplot(x='total_bill', y='tip', data=tips, ax=axes[1])
axes[1].set_title('Residual Plot')
plt.tight_layout()
plt.show()
# 3. 상관관계 히트맵
plt.figure(figsize=(10, 8))
corr = iris.drop('species', axis=1).corr()
mask = np.triu(np.ones_like(corr, dtype=bool)) # 상삼각 마스킹
sns.heatmap(corr, mask=mask, annot=True, fmt='.2f', cmap='coolwarm',
center=0, square=True, linewidths=.5)
plt.title('Feature Correlation Heatmap')
plt.show()
# 4. Pair Plot (다변량 관계)
sns.pairplot(iris, hue='species', diag_kind='kde',
plot_kws={'alpha': 0.6}, height=2.5)
plt.suptitle('Iris Dataset Pair Plot', y=1.02)
plt.show()
# 5. FacetGrid (조건부 시각화)
g = sns.FacetGrid(tips, col='time', row='smoker', hue='sex', height=4)
g.map_dataframe(sns.scatterplot, x='total_bill', y='tip')
g.add_legend()
g.fig.suptitle('Tips by Time, Smoker, and Sex', y=1.02)
plt.show()
# 6. 카테고리별 집계
plt.figure(figsize=(10, 6))
sns.barplot(x='day', y='total_bill', hue='sex', data=tips, errorbar='ci')
plt.title('Average Total Bill by Day and Sex')
plt.show()
# 이미지 저장
# plt.savefig('seaborn_plot.png', dpi=300, bbox_inches='tight')
시니어: "EDA할 때 pairplot 먼저 돌려봐요. 변수 간 관계가 한눈에 보여요."
주니어: "Matplotlib이랑 뭐가 달라요?"
시니어: "Seaborn은 통계 시각화에 특화되어 있고, DataFrame 컬럼명 그대로 쓸 수 있어서 코드가 훨씬 간결해요. 기본 스타일도 예쁘고요."
면접관: "탐색적 데이터 분석(EDA) 경험을 말씀해주세요."
지원자: "Seaborn으로 EDA 자동화 함수를 만들었습니다. histplot으로 수치형 변수 분포, countplot으로 범주형 변수 빈도, heatmap으로 상관관계를 자동 생성하고, pairplot으로 다변량 관계를 탐색했습니다. 이상치는 boxplot으로 시각화해 전처리 기준을 정했습니다."
리뷰어: "hue로 그룹을 나눌 때 palette 설정하면 색맹 친화적 색상 쓸 수 있어요. palette='colorblind' 추천해요."
개발자: "접근성 고려해서 바로 적용하겠습니다."