## 기본 라이브러리 세팅 import matplotlib.pyplot as plt import pandas as pd
## 한글 사용 가능하도독 폰트 설정 # window import matplotlib matplotlib.rcParams['font.family'] = 'Malgun Gothic' # 한글 폰트 사용 시 (-) 부호 깨짐 현상 해결 matplotlib.rcParams['axes.unicode_minus'] = False
# 타원 그래프 삽입을 위한 모듈 로딩 import matplotlib.patches as patches
위에 링크되어 있는 실습 자료를 기준으로
막대 그래플 그려 볼게요.
## 기초 데이터 x = ['2.21일', '2.22일', '2.23일', '2.24일'] y = [99573, 171452, 90016, 85890]
## 막대 높이를 나타내는 텍스트 추가 plt.bar(x,y) plt.ylim([min(y) - 10000, max(y) + 30000]) for i, txt in enumerate(y) : # 빈도수가 가장 높은 구간에 대해 별도 옵션 지정 if txt == max(y): plt.text(i, txt + 1500, txt, ha = 'center', color = 'red') # 빈도수가 가장 높은 구간 이외에는 기본값 설정 else: plt.text(i, txt + 1500, txt, ha = 'center')
이제 빈도수가 가장 높은 막대에 대해
타원을 삽입하여
강조하는 표현을 나타내 보도록 하겠습니다.
## 가장 빈도수가 높은 날짜에 타원 그래프 추가하기 plt.bar(x,y) plt.ylim([min(y) - 10000, max(y) + 30000]) for i, txt in enumerate(y) : if txt == max(y): plt.text(i, txt + 1500, txt, ha = 'center', color = 'red', size = 15) ellipse = patches.Ellipse((i,txt), width = 1.5, height = txt/4, color = 'red', fill = False, linestyle = 'dashed', linewidth = 2, alpha = 1) plt.gca().add_patch(ellipse) else: plt.text(i, txt + 1500, txt, ha = 'center')
댓글