1 분 소요

8. 막대 그래프(기본)

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.bar(labels, values)
<BarContainer object of 3 artists>

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

plt.bar(labels, values, color='r')
<BarContainer object of 3 artists>

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

plt.bar(labels, values, color=colors, alpha=0.5)
<BarContainer object of 3 artists>

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

plt.bar(labels, values)
plt.ylim(175, 195) # ylimit
(175.0, 195.0)

plt.bar(labels, values, width=0.3) # Bar 두께 조절
<BarContainer object of 3 artists>

plt.bar(labels, values, width=0.3)
plt.xticks(rotation=45) # x 축의 이름 데이터 각도를 45 도로 설정
plt.yticks(rotation=45) # y 축의 이름 데이터 각도를 45 도로 설정
(array([  0.,  50., 100., 150., 200.]),
 [Text(0, 0, ''),
  Text(0, 0, ''),
  Text(0, 0, ''),
  Text(0, 0, ''),
  Text(0, 0, '')])

labels = ['강백호', '서태웅', '정대만'] 
values = [190, 187, 184] 
ticks = ['1번학생', '2번학생', '3번학생'] # ticks 데이터로 바꿔서 표현 가능

plt.bar(labels,values)
plt.xticks(labels, ticks)
([<matplotlib.axis.XTick at 0x21fa189c400>,
  <matplotlib.axis.XTick at 0x21fa189c3d0>,
  <matplotlib.axis.XTick at 0x21fa1896f10>],
 [Text(0.0, 0, '1번학생'), Text(1.0, 0, '2번학생'), Text(2.0, 0, '3번학생')])

댓글남기기