My blog
[ML_week1_chap10] 시각화 본문
<파이썬 머신러닝 완벽가이드 chap 10 - 시각화 >
<맷플롯립의 pyplot 모듈의 이해>
In [17]:
import matplotlib.pyplot as plt
# x축 y축 직선그래프
plt.figure(figsize=(2,2) , facecolor='yellow')
plt.plot([1,2,3],[2,4,6]) # pyplot 모듈의 plot() 사용
plt.title("Hello plot")
# pyplot의 두가지 중요 요소 : Figure , Axes
# pyplot에서 Figure와 Axes 객체를 가져올 때 : plt.subplots() 이용
fig ,ax = plt.subplots(figsize=(2,2))
ax.plot([1,2,3],[2,4,6]) # Axes 객체로 plot
ax.set_title('Hellot plot')
plt.show()
Figure : 크기 조절 , 플롯을 화면에 최종적으로
Axes : 그림 그리는 메서드 , + x축 , y축, 타이틀
In [18]:
# 여러 개의 plot 을 가지는 subplot 생성
# 1개의 figure와 여러개의 Axes 생성 가능
fig,(ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=(1,0.5))
In [19]:
plt.figure(figsize=(2,2))
plt.plot([1,2,3],[2,4,6],color='red',marker='o',linestyle='dashed',linewidth=2,markersize=10)
plt.xlabel('x axis')
plt.ylabel('y axis')
# 눈금 값 회전
plt.xticks(rotation=90)
plt.show()
In [20]:
pylpot 의 plot() 함수는 선 그래프를 그릴 때 활용 : x좌표 y좌표 값으로 리스트 , ndarray ,DataFrame 등 모두 활용가능
import numpy as np
plt.figure(figsize=(4, 3))
x_val = np.arange(0,100)
y_val = 2*x_val
y_val2= 4*x_val
plt.plot(x_val,y_val,color='green',label='green line')
plt.plot(x_val,y_val2,color='red',label='red line')
plt.legend() #범례
Out[20]:
<matplotlib.legend.Legend at 0x1dbc7c51950>
In [21]:
x_val = np.arange(1, 6)
y_val = x_val * 2
y_value_01 = x_val * 1.5
# 1. Figure와 Axes 객체 생성
plt.figure(figsize=(4, 3))
ax = plt.axes()
# 2. 선 그래프와 막대 그래프 그리기
ax.plot(x_val, y_val, color='red', marker='o', linestyle='dashed', label='red line')
ax.bar(x_val, y_value_01, color='green', alpha=0.5, label='bar plot') # alpha는 투명도
# 3. 설정 (set_ 함수들 사용) vs plt.xlabe()
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_title('Hello plot')
# 4. 범례 표시
ax.legend()
# 5. 출력
plt.show()
In [22]:
fig , ax = plt.subplots(nrows=1,ncols=2,figsize=(6,3))
ax[0].plot(np.arange(1,10),2*np.arange(1,10),color='red',marker='o',label='red line')
ax[1].bar(np.arange(1,20),2*np.arange(1,20),color='green',label='bar plot')
ax[0].set_xlabel('ax[0] x asix')
ax[0].legend()
ax[1].legend()
ax[0].set_title('Hello line')
Out[22]:
Text(0.5, 1.0, 'Hello line')
<seaborn>
: matplotlib 기반 통계 시각화 라이브러리
ex ) 히스토그램 , 바 플롯 , 박스 플롯 , 바이올리 플롯 , 산점도 등등
In [27]:
# Seaborn
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
titanic_df=pd.read_csv('train.csv')
In [28]:
# 1 ) [ 히스토그램 ] : X : 연속형 값의 구간 y : 해당 구간의 도수 건수
# histplot() Axes 레벨
# displot() figure 레벨 , 크기는 displot에서 해야함
# ex ) sns.displot(titanic_df['Age'],kde=True,height-4,aspect=2)
plt.figure(figsize=(4,4))
sns.histplot(titanic_df['Age'],bins=20) # 구간개수
plt.show()
In [29]:
plt.figure(figsize=(4,4))
sns.histplot(x='Age', data = titanic_df,bins=30,kde=True)
# kde = True : 연속 확률 분포 곡선
Out[29]:
<Axes: xlabel='Age', ylabel='Count'>
In [33]:
#2 카운트 플롯 : 이산형 값을 막대그래프로 countplot()
plt.figure(figsize=(2,2))
sns.countplot(x='Pclass' , data = titanic_df)
Out[33]:
<Axes: xlabel='Pclass', ylabel='count'>
In [34]:
#3. 바 플롯 2차원 나타냄 (축 1: 이산 / 축2 : 연속 - 평균,총합,중앙)
# x 축 값이 이산형 값으로 종류가 많지 X 때 사용
# 숫자로 표현 , string X
plt.figure(figsize=(2,2))
sns.barplot(x='Pclass',y='Age', data = titanic_df)
# 수직 -> y가 연속값 수평 -> x가 연속값으로
Out[34]:
<Axes: xlabel='Pclass', ylabel='Age'>
In [36]:
plt.figure(figsize=(2,2))
#esimator 로 총합으로 설정
sns.barplot(x='Pclass',y='Survived',data=titanic_df ,ci=None,estimator=sum)
Out[36]:
<Axes: xlabel='Pclass', ylabel='Survived'>
In [38]:
# hue 인자 : 추가정보
plt.figure(figsize=(4,4))
sns.barplot(x='Pclass',y='Survived',hue='Sex',data=titanic_df)
Out[38]:
<Axes: xlabel='Pclass', ylabel='Survived'>
In [40]:
#3) 박스 플롯 - x or y 에 연속형 값 입력
plt.figure(figsize=(2,2))
sns.boxplot(x='Pclass',y='Age',hue='Sex',data=titanic_df)
Out[40]:
<Axes: xlabel='Pclass', ylabel='Age'>
In [41]:
#4) 바이올린 플롯 - 연속형 값 (히스토그램의 연속확률 분포곡선 + 박스플롯 )
plt.figure(figsize=(2,2))
sns.violinplot(x='Pclass',y='Age',hue='Sex',data=titanic_df)
Out[41]:
<Axes: xlabel='Pclass', ylabel='Age'>
In [55]:
cat_columns = ['Survived','Pclass','Sex']
#subplots 를 이용해 시본의 다양한 그래프 시각화
fig,axs = plt.subplots(nrows=1,ncols=len(cat_columns),figsize=(7,3))
for index,column in enumerate(cat_columns):
print('index: ',index)
sns.countplot(x=column,data=titanic_df,ax=axs[index])
index: 0
index: 1
index: 2
In [56]:
cat_columns=['Pclass','Sex','Embarked']
# 3개의 Axes 생성해 axs 객체 변수에 할당
# ax = 객체변수 주입
fig,axs = plt.subplots(nrows=1,ncols=len(cat_columns),figsize=(7,3))
for index,column in enumerate(cat_columns):
sns.barplot(x=column,y='Survived',data=titanic_df,ax=axs[index])
In [49]:
# 5 ) 산점도 둘다 연속적인 값이여야 의미 * style 제공
plt.figure(figsize=(4,4))
sns.scatterplot(x='Age',y='Fare',hue='Survived',style='Sex',data=titanic_df)
Out[49]:
<Axes: xlabel='Age', ylabel='Fare'>
In [47]:
# 6) 상관히트맵
# numeric_only=True 옵션을 넣으면 문자열 컬럼을 알아서 제외
plt.figure(figsize=(4,4))
corr = titanic_df.corr(numeric_only=True)
# 그 다음 히트맵을 그리면 정상적으로 나옵니다.
sns.heatmap(corr, annot=True, fmt='.1f', cbar=True)
Out[47]:
<Axes: >
'ML' 카테고리의 다른 글
| [ML_week4_chap3] 평가 (0) | 2026.02.10 |
|---|---|
| [ML_week3_chap5] 회귀 (0) | 2026.02.03 |
| [ML_week2_chap6] 차원축소 (0) | 2026.01.27 |
| [ML_week2_chap2] 사이킷런 (0) | 2026.01.24 |
| [ML_week1_chap1] Numpy , Pandas (0) | 2026.01.19 |