소품집

[KT AIVLE] seaborn library 옵션 정리 본문

AI/KT 에이블스쿨

[KT AIVLE] seaborn library 옵션 정리

sodayeong 2022. 8. 17. 23:27

seaborn 시각화 라이브러리 옵션 정리

 

1. 히스토그램 + kde 중첩

plt.figure(figsize=(13,6))

plt.subplot(1,2,1)
sns.histplot(data=titanic, x='Age', hue='Survived', bins=16,stat='density', kde=True, multiple='stack')
plt.title('seaborn 히스토그램')

plt.subplot(1,2,2)
plt.hist(data=titanic, x='Age', bins=20, color='pink', edgecolor='black', alpha=0.7, density=True,)
plt.xlabel('Age')
plt.title('matplot 히스토그램')

plt.grid()
plt.show()

- shade : 곡선 아래 공간의 투명도 설정(True / False)
- hue : 색깔에 따른 구분
- bins : bin의 개수
- kde = True : 커널밀도추정으로 
- legend : 범례표시 
- alpha : 투명도 조절
- density : matplot kde ver.

- grid : 배경 서식

 

 

2. 산점도 

기본은 plt.scatter로 확인할 수 있지만 모든 feature에 대해 확인하고 싶을 때는 sns.pairplot을 사용

- sns.scatterplot도 hue 옵션을 적용할 수 있다. 

Ozone과의 상관관계

sns.pairplot(air,hue='Ozone')
plt.show()

 

 

3. 조인트 플롯 + scatter or hisogram 

 

penguins = sns.load_dataset("penguins")

sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm")
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
sns.jointplot(
    data=penguins, x="bill_length_mm", y="bill_depth_mm",
    marker="+", s=100, marginal_kws=dict(bins=25, fill=False),
)
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="reg")

조인트플롯은 numeric feature 분포를 한 번에 확인할 수 있다.

 

 

 

4. Heatmap

 

temp1 = titanic.groupby(['Embarked','Pclass'], as_index = False)['PassengerId'].count()
temp2 = temp1.pivot('Embarked','Pclass', 'PassengerId')
print(temp2)

sns.heatmap(temp2, annot = True, cmap='Blues')
sns.heatmap(temp2, annot = True, fmt = 'd', linewidth = .2, cmap='Blues') 

plt.show()

- 범주형 데이터를 집계하고, 그 결과를 농도로 시각화 함. 

- 그래서 먼저 groupby와 pivot을 먼저 만들어야함. 

- annot : class 간 간격을 띄우기

- camp : Blues, GnBu 등 컬러 옵션은 다양하게 있다. 

 

https://chrisalbon.com/code/python/data_visualization/seaborn_color_palettes/

 

Color Palettes in Seaborn

Color palettes in Seaborn.

chrisalbon.com

https://seaborn.pydata.org/index.html

 

seaborn: statistical data visualization — seaborn 0.11.2 documentation

Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. For a brief introduction to the ideas behind the library, you can read the introductory note

seaborn.pydata.org

palettes 참조 링크 

728x90
Comments