adding histogram example
parent
f8c316559e
commit
00f7caf69b
@ -0,0 +1,37 @@
|
|||||||
|
"""
|
||||||
|
Histogram: a diagram consisting of rectangles whose area is proportional to the frequency
|
||||||
|
of a variable and whose width is equal to the class interval.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
|
||||||
|
grades = [83, 95, 91, 87, 70, 0, 85, 82, 100, 67, 73, 77, 0]
|
||||||
|
|
||||||
|
|
||||||
|
def decile(grade: int) -> int:
|
||||||
|
"""Returns the decile the grade falls into.
|
||||||
|
|
||||||
|
Floor division must be used.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return grade // 10 * 10
|
||||||
|
|
||||||
|
|
||||||
|
histogram = Counter(decile(grade) for grade in grades)
|
||||||
|
|
||||||
|
# Note We do not need to use the alignment tricks to center our bars
|
||||||
|
# That was a hold over from the previous Matplob lib API
|
||||||
|
plt.bar(x=histogram.keys(), height=histogram.values(), width=8)
|
||||||
|
|
||||||
|
# Used to set the axis scale x-min, x-max, y-min, y-max
|
||||||
|
# We want to fully show the x axis
|
||||||
|
# You usually always want your y to start at 0
|
||||||
|
plt.axis([-5, 105, 0, 5])
|
||||||
|
|
||||||
|
plt.xticks(range(0, 110, 10))
|
||||||
|
plt.xlabel("Decile")
|
||||||
|
plt.ylabel("# of Students")
|
||||||
|
plt.title("Distribution of Exam 1 Grades")
|
||||||
|
plt.show()
|
Loading…
Reference in New Issue