최대 1 분 소요

9. 막대 그래프(심화)

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family'] = 'Malgun Gothic' # 글자 폰트
matplotlib.rcParams['font.size'] = 15 # 글자 크기
matplotlib.rcParams['axes.unicode_minus'] = False # 한글 폰트 사용 시, 마이너스 글자가 깨지는 현상을 해결
labels = ['강백호', '서태웅', '정대만'] # 이름
values = [190, 187, 184] # 키

plt.barh(labels, values) # barh
<BarContainer object of 3 artists>

labels = ['강백호', '서태웅', '정대만'] # 이름
values = [190, 187, 184] # 키

plt.barh(labels, values) # barh
plt.xlim(175, 195)
(175.0, 195.0)

bar = plt.bar(labels, values)
bar[0].set_hatch('/') # /////
bar[1].set_hatch('x') # xxxxx
bar[2].set_hatch('..') # ......

bar = plt.bar(labels, values)
plt.ylim(175, 195)

for idx, rect in enumerate(bar): # idx:인덱스, rect: 각 바에 대한 크기,위치 정보
    plt.text(idx, rect.get_height()+0.5, values[idx], ha='center', color='green')

댓글남기기