goal
점수를 받아 bar그래프로 histogram을 그려 구간별 항목의 개수를 알아본다.
참고자료
조엘 그루스(김한결, 한성주, 박은정 옮김), "밑바닥부터 시작하는 데이터 과학 2판", O'REILLY
수정, 개선이 필요할 경우 지적해주시면 감사하겠습니다.
1. 목표
- 성적이 주어졌을 때 아래와 같은 histogram을 시각화한다.
- grades = [83, 95, 91, 87, 70, 0, 85, 82, 100, 67, 73, 77, 0]
1-1 아이디어
title은 Distribution fo Exam 1 Grades
- plt.title("Distribution fo Exam 1 Grades")
label의 설정
- ylabel -> # of Students
- plt.ylabel("# of Students")
- xlabel -> Decile
- plt.xlabel("Decile")
- ylabel -> # of Students
x축은 10단위로 0부터 100까지 설정되어 있다.
1-2 코드내용
from collections import Counter
import matplotlib.pyplot as plt
grades = [83, 95, 91, 87, 70, 0, 85, 82, 100, 67, 73, 77, 0]
# dictionary로 나오는 값에 따른 집계
histogram = Counter(min(grade // 10 * 10 ,90) for grade in grades)
plt.bar([x+5 for x in histogram.keys()], # 각 막대를 우측으로 5만큼씩 이동
histogram.values(), # 각 막대의 높이 지정
10, # 막대의 너비는 10
edgecolor = (0,0,0,0)) # 막대 테두리 색상
plt.axis([-5, 105, 0, 5]) # x축은 -5부터 105까지
# y축은 0부터 5까지
plt.xticks([10 * i for i in range(11)]) # xticks, x의 눈금을 0부터 10단위로 100까지 나타냄
plt.xlabel("Decile")
plt.ylabel("# of Students")
plt.title("Distribution fo Exam 1 Grades")
plt.show()