[파이썬] seaborn 그룹화된 데이터의 통계 시각화

데이터 시각화는 데이터를 이해하고 분석하는 데 매우 중요한 요소입니다. Seaborn은 Python의 데이터 시각화 라이브러리 중 하나로, 아름답고 동시에 강력한 시각화 도구를 제공합니다. 특히 seaborn을 사용하여 그룹화된 데이터의 통계를 시각화할 수 있습니다.

이 블로그 포스트에서는 seaborn을 사용하여 그룹화된 데이터의 통계 시각화를 만드는 방법을 배워보겠습니다. 아래는 몇 가지 예시 코드입니다.

예시 코드 1: 막대 그래프

import seaborn as sns
import matplotlib.pyplot as plt

# 데이터셋 불러오기
tips = sns.load_dataset("tips")

# 그룹화된 데이터의 통계 계산하기
grouped = tips.groupby("day")["total_bill"].mean().reset_index()

# 막대 그래프 그리기
sns.barplot(x="day", y="total_bill", data=grouped)

# 그래프 제목 설정
plt.title("요일별 평균 지불 금액")

# 그래프 출력
plt.show()

이 코드는 tips 데이터셋에서 “day” 열로 그룹화된 “total_bill” 열의 평균을 계산하고, 이를 막대 그래프로 시각화합니다.

예시 코드 2: 상자 그림

import seaborn as sns
import matplotlib.pyplot as plt

# 데이터셋 불러오기
tips = sns.load_dataset("tips")

# 그룹화된 데이터의 통계 계산하기
grouped = tips.groupby("day")["total_bill"].mean().reset_index()

# 상자 그림 그리기
sns.boxplot(x="day", y="total_bill", data=tips)

# 그래프 제목 설정
plt.title("요일별 지불 금액 분포")

# 그래프 출력
plt.show()

이 코드는 tips 데이터셋에서 “day” 열로 그룹화된 “total_bill” 열의 분포를 상자 그림으로 시각화합니다.

예시 코드 3: 히트맵

import seaborn as sns
import matplotlib.pyplot as plt

# 데이터셋 불러오기
flights = sns.load_dataset("flights")

# 데이터 재구성하기
pivot = flights.pivot_table(values="passengers", index="month", columns="year")

# 히트맵 그리기
sns.heatmap(pivot, cmap="YlGnBu")

# 그래프 제목 설정
plt.title("연도별 월간 승객 수")

# 그래프 출력
plt.show()

이 코드는 flights 데이터셋을 사용하여 연도별 월간 승객 수를 히트맵으로 시각화합니다.

이처럼 seaborn을 사용하면 그룹화된 데이터의 통계를 직관적이고 아름답게 시각화할 수 있습니다. 더 많은 seaborn 기능을 탐색하고 원하는 시각화를 구현해보세요!