Python / / 2022. 9. 30. 17:48

Python - Seaborn 데이터셋, 그래프 그리기

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
'''
Seaborn은 Matplotlib의 기능과 스타일을 확장한 파이썬 시각화 도구의 고급 버전
비교적 단순한 인터페이스 제공으로 어렵지 않다

''''''
회귀선이 있는 산점도 

-sns.regplot()함수는 서로 다른 2개의 연속 변수 사이의 산점도를 그리고 선형회귀분석에 의한 회귀선을 함께 표시 
-fig_reg=False 옵션은 회귀선을 생략 

< 타이타닉 데이터셋 사용 > 

Seaborn 제공 타이타닉 데이터셋 가져오기(titanic 저장)

스타일 테마 설정 *(5가지 : darkgrid,whitegrid,dark,white,ticks)
sns.set_style('darkgrid')
그래프 객체 생성 (figure에 2개 서브 플롯 생성)
ax1에 그래프 그리기 - 선형 회귀선 표시(fit_reg = True)
ax2에 그래프 그리기 - 선형 회귀선 미표시(fit_reg = False)
'''

titanic = sns.load_dataset('titanic')
# Seaborn 제공 titanic 데이터 셋 사용

sns.set_style('white')
# 스타일 테마 설정
# fig = plt.figure(figsize=(10,5))
# ax1 = fig.add_subplot(1,2,1)
# ax2 = fig.add_subplot(1,2,2)

# #ax1 선형 회귀선 표시
# sns.regplot(x = 'age',
#            y = 'fare',
#            data = titanic,
#            ax = ax1,
#            fit_reg = True)

# #ax2 선형 회귀선 미표시
# sns.regplot(x = 'age',
#            y = 'fare',
#            data = titanic,
#            ax = ax2,
#            fit_reg = False)


# print(titanic)
# plt.show()

'''히스토그램 커널 밀도 그래프'''
# fig = plt.figure(figsize=(10,5))
# ax1 = fig.add_subplot(1,3,1)
# ax2 = fig.add_subplot(1,3,2)
# ax3 = fig.add_subplot(1,3,3)

# sns.distplot(titanic['fare'],ax = ax1)
# sns.distplot(titanic['fare'],ax = ax2,hist=False)
# sns.distplot(titanic['fare'],ax = ax3,kde=False)
# plt.show()

'''히트 맵

2개의 범주형 변수를 각각 x,y축에 넣고 데이터를 매트릭스 형태로 분류 
데이터프레임을 피벗테이블로 정리할 때 한 변수를 행 인덱스, 나머지 변수를 열 이름으로 설정 
aggfunc='size' 옵션은 데이터 값을 크기를 기준으로 집계 한다는 뜻 

-table =titanic.pivot_table(index=['sex'],columns=['class'],aggfunc='size')
-heatmap()
-데이터 프레임, 데이터 값 표시 여부, 정수형 포맷, 컬러맵,구분선, 컬러바 표시 여부 
'''

table =titanic.pivot_table(index=['sex'],columns=['class'],aggfunc='size')
sns.heatmap(data=table,annot=True,fmt='d',linewidths=1,cbar=False,cmap='Pastel2')
plt.show()

'Python' 카테고리의 다른 글

Python - folium 사용 지도데이터  (0) 2022.10.01
Python -seaborn 그래프 -2  (0) 2022.10.01
Python - Pandas 여러 그래프 시각화  (0) 2022.09.30
Python - Pandas 그래프 시각화 -2  (1) 2022.09.30
Python - Pandas 그래프 꾸미기 !  (0) 2022.09.30
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유