Seaborn

API reference - seaborn 0.11.1 documentation

Seaborn은 Matplotlib의 기능과 스타일을 확장한 파이썬 시각화 도구의 고급 버전이다.

데이터셋 가져오기

Seaborn에서는 titanic이라는 데이터셋을 제공한다.

import seaborn as sns

titanic = sns.load_dataset('titanic')

print(titanic.head())
print()
print(titanic.info())
		survived  pclass     sex  ...  embark_town  alive  alone
0         0       3    male  ...  Southampton     no  False
1         1       1  female  ...    Cherbourg    yes  False
2         1       3  female  ...  Southampton    yes   True
3         1       1  female  ...  Southampton    yes  False
4         0       3    male  ...  Southampton     no   True

[5 rows x 15 columns]

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 15 columns):
 #   Column       Non-Null Count  Dtype
---  ------       --------------  -----
 0   survived     891 non-null    int64
 1   pclass       891 non-null    int64
 2   sex          891 non-null    object
 3   age          714 non-null    float64
 4   sibsp        891 non-null    int64
 5   parch        891 non-null    int64
 6   fare         891 non-null    float64
 7   embarked     889 non-null    object
 8   class        891 non-null    category
 9   who          891 non-null    object
 10  adult_male   891 non-null    bool
 11  deck         203 non-null    category
 12  embark_town  889 non-null    object
 13  alive        891 non-null    object
 14  alone        891 non-null    bool
dtypes: bool(2), category(2), float64(2), int64(4), object(5)
memory usage: 80.7+ KB
None

regplot() : 회귀선이 있는 산점도

서로 2개의 연속 변수사이의 관계를 볼때유용하다.

fit_reg = False 옵션을 설정하면 회귀선을 가릴 수 있다.

import seaborn as sns
import matplotlib.pyplot as plt

titanic = sns.load_dataset('titanic')

sns.set_style('darkgrid')

fig = plt.figure(figsize=(12, 5))
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)

sns.regplot(x='age', y='fare',
            data=titanic,
            ax=ax1,
            color='orange')

sns.regplot(x='age', y='fare',
            data=titanic,
            ax=ax2,
            fit_reg=False)

plt.show()

Visualizing distributions of data

displot() : figure-level 함수

먼저 Figure단계에서 포괄적으로 다루는 displot()메서드가 있다. 아래의 문서에 잘 정리되어 있다

Visualizing distributions of data - seaborn 0.11.1 documentation

Axes-level 함수들