import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
# 한글 폰트 세팅
font_path = "./malgun.TTF"
font = font_manager.FontProperties(fname=font_path).get_name()
rc('font', family=font)
'''
< 범주형 데이터 산점도 >
-범주형 변수에 들어 있는 각 범주별 데이터 분포 확인
-stripplot() 함수, swarmplot()함수
● stripplot: 데이터 포인트가 중복되어 범주별 분포를 그림
● swarmplot: 데이터의 분산까지 고려하여, 데이터 포인트가 서로 중복되지 않도록 그림 즉, 데이터가 퍼져있는 정도를 입체적으로 표현
'''
#그래프 객체 생성
titanic = sns.load_dataset('titanic')
#스타일 테마 설정 (5가지: darkgrid, whitegrid, dark, white, ticks)
sns.set_style('whitegrid')
# 그래프 객체 생성 (figure에 2개의 서브 플롯을 생성)
fig = plt.figure(figsize=(15, 5))
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
# 이산형 변수의 분포 - 데이터 분산 미고려
sns.stripplot(x="class", #x축 변수
y="age", #y축 변수
data=titanic, #데이터셋 - 데이터프레임
ax=ax1) #axe 객체 - 1번째 그래프
# 이산형 변수의 분포 - 데이터 분산 고려 (중복 X)
sns.swarmplot(x="class", #x축 변수
y="age", #y축 변수
data=titanic, #데이터셋 - 데이터프레임
ax=ax2) #axe 객체 - 2번째 그래프
# 차트 제목 표시
ax1.set_title('Strip Plot')
ax2.set_title('Strip Plot')
plt.show()
'''막대 그래프
barplot() 함수는 지정한 변수의 평균을 계산하여 그림
-데이터의 개수가 아닌 평균을 계산한다
-막대그래프 위에 덧그려진 검은 선은 95% 신뢰 구간
'''
#데이터셋 가져오기 :
titanic = sns.load_dataset('titanic')
#sns.barplot
sns.set_style('whitegrid')
#그래프 객체 생성 : figure에 3개의 서브 플롯을 생성
fig = plt.figure(figsize=(15, 5))
ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)
#barplot
sns.barplot(x='sex', y='survived', data=titanic, ax=ax1)
sns.barplot(x='sex', y='survived', hue='class', data=titanic, ax=ax2)
#변수 할당하고 hue 옵션을 추가하여 누적
sns.barplot(x='sex', y='survived', hue='class', dodge=False, data=titanic, ax=ax3)
ax1.set_title('titanic - SEX')
ax2.set_title('titanic - SEX/CLASS')
ax3.set_title('titanic - SEX/CLASS(stacked)')
plt.show()
'''빈도 그래프
countplot()함수는 각 범주에 속하는 데이터의 개수를 막대그래프로 표현
'''
fig = plt.figure(figsize=(15, 5))
ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)
#SET 옵션은 스타일 변환
sns.countplot(x='class', palette='Set1',data=titanic, ax=ax1)
sns.countplot(x='class', palette='Set2',hue='who',data=titanic, ax=ax2)
sns.countplot(x='class', palette='Set3',hue='who',data=titanic,dodge=False, ax=ax3)
ax1.set_title('titanic - Class')
ax2.set_title('titanic - 2')
ax3.set_title('titanic - 3')
plt.show()
'''
< 박스플롯, 바이올린 플롯 >
boxplot()은 범주형 데이터 분포와 주요 통계 지표 함께 제공
-분산 파악 어려움
그래서 커널 밀도 함수 그래프를 Y축 방향에 추가하여 바이올린 플롯 violinplot()
'''
fig = plt.figure(figsize=(15, 5))
ax1 = fig.add_subplot(1, 4, 1)
ax2 = fig.add_subplot(1, 4, 2)
ax3 = fig.add_subplot(1, 4, 3)
ax4 = fig.add_subplot(1, 4, 4)
#SET 옵션은 스타일 변환
sns.boxplot(x='alive', y='age',data=titanic, ax=ax1)
sns.boxplot(x='alive', y='age',hue='sex',data=titanic, ax=ax2)
sns.violinplot(x='alive', y='age',data=titanic, ax=ax3)
sns.violinplot(x='alive', y='age',hue='sex',split=True,data=titanic, ax=ax4)
ax1.set_title('titanic - 1')
ax2.set_title('titanic - 2')
ax3.set_title('titanic - 3')
ax4.set_title('titanic - 4')
plt.show()
'''
조인트 그래프
jointplot() 함수는 산점도를 기본으로 표시하고, x-y축에 각 변수에 대한 히스토그램을 동시에 보여줌
두 변수의 관계와 데이터가 분산되어 있는 정도를 한눈에 파악하기에 장점
'''
fig = plt.figure(figsize=(15, 5))
ax1 = fig.add_subplot(1, 4, 1)
ax2 = fig.add_subplot(1, 4, 2)
ax3 = fig.add_subplot(1, 4, 3)
ax4 = fig.add_subplot(1, 4, 4)
#조인트 그래프 -산점도(기본값) (fare,age,data)
sns.jointplot(x='fare', y='age',data=titanic, ax=ax1)
#회귀선(fare,age,kind='reg',data)
sns.jointplot(x='fare', y='age',kind='reg',data=titanic, ax=ax2)
#육각 그래프(fare, age,hex,data)
sns.jointplot(x='fare', y='age',kind='hex',data=titanic, ax=ax3)
#커널 밀집 그래프(fare,age,kde,data)
sns.jointplot(x='fare', y='age',kind='kde',data=titanic, ax=ax4)
#차트 제목표시
plt.show()
sns.FacetGrid(data=titanic, row='survived', col='who')
pair=titanic[['age','pclass','fare']]
sns.pairplot(pair)