📒 Today I Learn/🐍 Python

[데이터 시각화] 파이썬으로 그래프 그리기 : seaborn (4) 상관관계 그래프, 조인트 그래프 그리기

ny:D 2024. 6. 3. 09:35

상관관계 그래프 그리기

💽 활용 데이터셋 - seaborn diamonds

diamonds2

diamonds = sns.load_dataset('diamonds')

# diamonds에서 numeric 변수만 선택
diamonds2 = diamonds.select_dtypes(include='number')
diamonds2.head()

📊 상관관계 히트맵 그리기

heatmap_diamonds = sns.heatmap(diamonds2.corr(), annot = True, cmap = 'RdPu')
heatmap_diamonds.set(title='corr plot')

조인트 그래프 그리기

💽 활용 데이터셋 - seaborn healthexp

health_exp

health_exp = sns.load_dataset('healthexp')
health_exp = health_exp.groupby('Year')[['Spending_USD','Life_Expectancy']].mean().reset_index()
health_exp.head()

📊 조인트 그래프 그리기

#두 변수에 분포에 대한 분석시 사용
sns.jointplot(x=health_exp['Spending_USD'], y=health_exp['Life_Expectancy'], 
              kind = 'hist', 
              ratio=5,
              marginal_kws=dict(bins=10, fill=False)
              )
  • kind  :  내부 그래프에 그릴 그래프의 종류를 선택하기
    • default : scatterplot
    • reg : regression plot
    • hist : histogram
    • hex : hexagon
  • ratio : 축 내부에 있는 그래프의 사이즈와 축 외부에 그려지는 그래프의 비율 (default = 5)
  • marginal_kws=dict() : 축 외부에 그려지는 그래프 세부 설정
    • bins : 외부 히스토그램의 개수 조절
    • fill = False : 색 채움 없앰.