Seaborn
matplotlib을 감싸서 만든 보다 쉬운 파이썬 시각화 패키지
matplotlib보다 쉬운 코드로 그릴 수 있음
matplotlib의 명령어를 그대로 사용할 수 있음
데이터의 통계적인 부분을 살펴볼 때 강점이 있다.
import 및 준비
# COLAB 환경을 위한 나눔고딕 한글폰트 설치 --> 진행 후, 런타임 > 런타임 다시 시작을 해 주세요. 다시 시작 후에는 이 부분 실행 X
!sudo apt-get install -y fonts-nanum
!sudo fc-cache -fv
!rm ~/.cache/matplotlib -rf
from matplotlib import pyplot as plt
import seaborn as sns
sns.set()
plt.rc('font', family=['NanumBarunGothic', 'Malgun Gothic', 'AppleGothic'])
- sns.set() 은 Seaborn을 사용하기전에 환경을 set하는 의미
내장 데이터셋
# 내장 데이터 셋 불러오기
tips = sns.load_dataset('tips')
tips.sample(10) # dataframe의 랜덤한 10행 살펴보기
- sns.load_dataset()
- tips 말고도 다른 데이터셋들도 있다.
그래프 생성
countplot
sns.countplot(data=tips, x='day');
plt.title('요일별 팁을 준 횟수');
- countplot은 지정column의 횟수를 그래프로 그려준다.
- plt 명령어와 같이 쓸 수 있음.
boxplot
sns.boxplot(data=tips, x='day', y='tip');
plt.title('요일별 팁의 Box Plot');
- 4분위 도표
scatterplot
sns.scatterplot(data=tips, x='total_bill', y='tip');
plt.title('식사 금액과 팁의 관계');
jointplot
sns.jointplot(data=tips, x='total_bill', y='tip');
plt.suptitle('식사 금액과 팁의 관계 Joint Plot', y=1.02); # y 값으로 제목 위치 조정
- 복합 그래프 ⇒ 데이터 분포까지 볼 수 있다.
- plt.suptitle() : 대제목
violinplot
sns.violinplot(data=tips, x='day', y='tip', hue='sex');
plt.title('요일 별, 성별 팁의 Violin Plot');
- hue 옵션으로 데이터를 추가 할 수 있다. 다른 plot에도 적용 가능.
distplot
sns.distplot(tips.tip, kde=True, rug=True);
plt.title('팁의 분포');
- 데이터의 분포를 볼 수 있다.
- 하나의 데이터(column) 분포를 볼 수 있는 그래프이므로 Series를 넣어준다.
- kde : 막대히스토그램을 확률분포함수로 나타내어준다. 일정 수치 사이의 간격 면적
- default = True
- rug : 데이터가 어디에 얼마나 위치해있는지를 바코드로 나타낸다.
다양한 파이썬 시각화 관련 그래프가 있는 사이트
https://python-graph-gallery.com/
Python Graph Gallery | The Python Graph Gallery
The Python Graph Gallery displays hundreds of charts made with Python, always with explanation and reproduciible code
www.python-graph-gallery.com
Seaborn 튜토리얼
https://seaborn.pydata.org/tutorial.html
User guide and tutorial — seaborn 0.12.1 documentation
seaborn.pydata.org
'퀀트' 카테고리의 다른 글
09. 데이터 파일(CSV) 읽어서 그래프 그리기 + 이동평균선 추가 (0) | 2022.12.28 |
---|---|
08. 데이터 분석 : EDA (1) | 2022.12.26 |
06. Matplotlib / Pandas 에서 plot 생성하기 (0) | 2022.12.01 |
05. 시각화를 위한 그래프들 (0) | 2022.12.01 |
04. DataFrame : 생성, 연산, 정렬, 병합, 그룹, 쿼리 (1) | 2022.11.30 |