From 00f7caf69b86000bf1abeffb84261396fbe3c5ba Mon Sep 17 00:00:00 2001 From: androiddrew Date: Sat, 9 Feb 2019 11:55:22 -0500 Subject: [PATCH] adding histogram example --- ch3/bar_histogram.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ch3/bar_histogram.py diff --git a/ch3/bar_histogram.py b/ch3/bar_histogram.py new file mode 100644 index 0000000..3a6a4fe --- /dev/null +++ b/ch3/bar_histogram.py @@ -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()