Obdisian에 정리한 markdown을 html export한 뒤, Tistory로 옮긴 게시글.
last update : 2026-01-19
Seaborn
데이터 처리 및 시각화
Matplotlib 기반 -> matplotlib 문법을 많이 가지고 있다.
Matplotlib보다 직관적이다.
%matplotlib inline # Jupyter Notebook에서 사용할 때 그래프 그리기
import matplotlib.pyplot as plt
import seaborn as sns
Figure Level Function
Matplotlib API의 기능을 대체하기 위해 만들어졌다.
matplotlib 기능 대신에 해당 함수에서 parameter로 관련 기능을 대신한다.
Subplots
Seaborn은 matplotlib과 반대로, method에서 ax를 할당한다.
cat_cols = ['Pclass', 'Sex', 'Embarked']
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(8, 4))
for idx, col in enumerate(cat_cols):
sns.barplot(x=col, y='Survived', data=df, ax=axes[idx]) # Set ax here
Color Palettes
Color Palettes
default는 rocket이다.
sns.color_palette("rocket", as_cmap=True)
[ plots ]
| x axis | y axis | 주요 목적 | |
|---|---|---|---|
| Histogram | Continuous | None (Frequency) | 구간을 나눠 분포 확인 |
| Count Plot | Discrete (Categorical) | None (Count) | 각 범주별 데이터 개수 확인 |
| Bar Plot | Discrete (Categorical) | Continuous (Summary) | 범주별 수치(평균 등) 비교 |
| Box Plot | Discrete (Categorical) | Continuous | 범주별 데이터의 사분위수 및 이상치 확인 |
| Violin Plot | Discrete (Categorical) | Continuous | Box Plot 정보 + 데이터의 밀도(분포) 확인 |
| Scatter Plot | Continous | Continous | 두 변수 간의 관계, 경향성(상관성) 확인 |
| Correlation Heatmap | Correlation | Correlation | 두 속성(x/y)간의 상관관계를 0-1로 확인 |
Histogram
plt.figure(figsize=(8, 2))
sns.histplot(df['col1'], bins=20) # Dataframe을 이용할 수도 # bin=막대개수
sns.histplot(x='col1', data=df, bins=20, kde=True) # kde=추세곡선
# Figure Level Function
sns.displot(x='col1', data=df, bins=20, kde=True,
height=4, aspect=2) # plt의 figsize를 height, aspect(width/height 비율)로 대체한다.
plt.show()
Count Plot
sns.countplot(x='Pclass', data=df)
plt.show()
Bar Plot
x와 y 중 최소 하나는 numerical value여야 한다.
# 기본 평균
sns.barplot(x='Pclass', y='Age', data=df) # Age: 0~. y값이 숫자면, 자동으로 세로로 그려짐.
sns.barplot(x='Pclass', y='Sex', data=df) # Sex: 'Male'/Female'. y값이 문자열이면, 자동으로 가로로 그려짐.
sns.barplot(x='Pclass', y='Age', data=df, ci=None, estimator=sum) # 평균이 아니라 총합을 보여줌.
sns.barplot(x='Pclass', y='Age', data=df, hue='Sex') # 세부 항목을 `Sex`로 또다시 분류함.
plt.show()
Box Plot
sns.boxplot(y='Age', data=df) # `Age`에 대한 box plot을 그림
sns.boxplot(x='Pclass', y ='Age', data=df) # Pclass 값 별로, `Age`에 대한 box plot을 그림.
sns.boxplot(x='Pclass', y ='Age', hue='Sex', data=df) # 세부 항목을 `Sex`로 또다시 분류함.
plt.show()
Violin Plot
sns.violinplot(y='Age', data=df)
sns.violinplot(x='Pclass', y ='Age', data=df) # Pclass 값 별로, `Age`에 대한 box plot을 그림.
sns.violinplot(x='Pclass', y ='Age', hue='Sex', data=df) # 세부 항목을 `Sex`로 또다시 분류함.
plt.show()
Scatter Plot
sns.scatterplot(x='Age', y='Fare', data=df)
sns.scatterplot(x='Age', y='Fare', hue='Survived', data=df) # 점의 색을 Survived(0/1)에 따라.
sns.scatterplot(x='Age', y='Fare', style='Sex', data=df) # 점의 모양을 Sex(male/female)에 따라.
plt.show()
Correlation Heatmap
Dataframe을 입력으로 받는다.
correlation_df = df.corr() # Dataframe.corr() 을 통해 correlation의 Dataframe 생성.
sns.heatmap(correlation_df)
sns.heatmap(correlation_df, annot=True, fmt='.1f') # 각 cell의 값을 나타냄. 소수점 1자리까지.
sns.heatmap(correlation_df, cbar=False) # 오른쪽의 color bar를 숨김.
plt.show()
'Programming Language > Python' 카테고리의 다른 글
| Sklearn (0) | 2026.01.26 |
|---|---|
| Matplotlib (0) | 2026.01.20 |
| Pandas (0) | 2026.01.20 |
| Numpy (0) | 2026.01.17 |