IT_developers

Python 데이터 분석(주피터노트북) - Matplotlib(subplots() 여러 차트) 본문

Python

Python 데이터 분석(주피터노트북) - Matplotlib(subplots() 여러 차트)

developers developing 2022. 11. 17. 12:00

matplotlib 기본 세팅

  • 라이브러리 : import matplotlib.pyplot as plt
  • 한글처리
    • plt.rcParams['font.family'] = 'Malgun Gothic'
    • plt.rcParams['axes.unicode_minus'] = False

 

데이터 가져오기

 

차트 알아보기 : subplots()

 

여러 개의 차트 만들기

 

1) 차트 만들기

 

2) 첫번째 차트 만들기

  • axes[0,0]

 

3) 두번째 차트 만들기

  • axes[0,1]

 

4) 세번째 차트 만들기

  • axes[1,0]

 

5) 네번째 차트 그리기

  • axes[1,1]

 

6) 정리

# 전체 제목 지정
fig, axes = plt.subplots(2,2, figsize=(15,10))
fig.suptitle("여러 개의 그래프")

# 첫번째 그래프 작성(국어, 막대차트)
axes[0,0].bar(student_df['이름'], student_df['국어'], label='국어')
# 제목
axes[0,0].set_title('학생별 국어 점수')
# 범례
axes[0,0].legend()
# 이름 지정
axes[0,0].set_xlabel('이름')
axes[0,0].set_ylabel('점수')
# 그리드 주기
axes[0,0].grid(linestyle='--', linewidth=0.5)
# 배경색
axes[0,0].set_facecolor('lightyellow')

# 두번째 그래프 작성(영어, 수학, 선차트)
axes[0,1].plot(student_df['이름'], student_df['영어'], label='영어')
axes[0,1].plot(student_df['이름'], student_df['수학'], label='수학')
# 범례
axes[0,1].legend()
# 제목
axes[0,1].set_title('학생별 영어 수학 점수')
# 이름 지정
axes[0,1].set_xlabel('이름')
axes[0,1].set_ylabel('점수')

# 세번째 그래프 작성(키, 수평 바 차트)
axes[1,0].barh(student_df['이름'], student_df['키'])
# 이름 지정
axes[1,0].set_xlabel('이름')
axes[1,0].set_ylabel('키')
# 제목
axes[1,0].set_title('학생별 키')

# 네번째 그래프 작성(사회, 선 차트, 색상)
axes[1,1].plot(student_df['이름'], student_df['사회'], label='사회', color='g')
# 범례
axes[1,1].legend()
# 제목
axes[1,1].set_title('학생별 사회 점수')
# 이름 지정
axes[1,1].set_xlabel('이름')
axes[1,1].set_ylabel('사회')

Comments