개발공부/SK Networks Family AI bootcamp 강의노트

14일차 [ 데이터 시각화 ]

HyunJung_Jo 2025. 2. 3. 16:46

통계 분석 => 데이터 시각화

 

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

df_anscombe = sns.load_dataset("anscombe")

groups=df_anscombe.groupby('dataset')
df_1=groups.get_group('I')
df_2=groups.get_group('II')
df_3=groups.get_group('III')
df_4=groups.get_group('IV')

# 통계 분석
# 각 dataset별 x,y에 대한 평균, 표준편차,분산 
df_anscombe.groupby('dataset').agg(['mean','std','var'])

x	y
mean	std	var	mean	std	var
dataset						
I	9.0	3.316625	11.0	7.500909	2.031568	4.127269
II	9.0	3.316625	11.0	7.500909	2.031657	4.127629
III	9.0	3.316625	11.0	7.500000	2.030424	4.122620
IV	9.0	3.316625	11.0	7.500909	2.030579	4.123249

# 그래프 분석
# 그래프를 그릴 도화지 선언
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
ax1.plot(df_1['x'],df_1['y'],'o')
ax2.plot(df_2['x'],df_2['y'],'o')
ax3.plot(df_3['x'],df_3['y'],'o')
ax4.plot(df_4['x'],df_4['y'],'o')

fig.suptitle('Anscombe Data')
ax1.set_title('dataset1')
ax2.set_title('dataset2')
ax3.set_title('dataset3')
ax4.set_title('dataset4')

fig

 

직관적으로 볼 수 있어서 인사이트 얻기 편하다!!

matplotlib

그렇다, 데이터분석은 쉽다.

가 아니고

matplotlib 원리는 Oil Painting과 같다. 

덧붙이고 덧붙인다.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

plt.figure() # 도화지: graph & title
plt.axes() # 도화지 안에 그래프가 들어갈 공간
plt.show() # 실제 화면에 표시하는 명령어

# 그래프 그리는 함수
plt.plot(np.arange(2,7))
plt.show()

plt.figure(figsize=[15,5]) # 도화지 너비,넓이
plt.plot(np.arange(5)) # 첫 그래프
plt.plot(np.arange(2,7)) # 두번째 그래프
plt.show()

import koreanize_matplotlib
# fig 전체 도화지
# ax 그래프가 그려질 공간
# subplots(row,col,fig_size)
fig,ax = plt.subplots(2,2,figsize=[15,10])
fig.suptitle('연습용이에요')
ax[0,0].set_title('첫번째 그래프')
ax[0,1].set_title('두번째 그래프')
ax[1,0].set_title('세번째 그래프')
ax[1,1].set_title('네번째 그래프')
# 그래프별로 간격 넓히기
plt.tight_layout()
ax[0,0].plot(np.arange(5))
ax[0,1].plot(np.arange(2,7))
ax[1,0].plot(np.arange(5))
ax[1,1].plot(np.arange(2,7))
plt.show()