Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram with grades

I need to make a histogram in Python with grades distribution. I use matplotlib.

import matplotlib.pyplot as plt

plt.title('Histogram Grades')
data = [1, 3, 10, 8, 9, 5, 3, 7, 7, 3, 1, 3, 10, 8, 9, 5, 3, 7, 7, 3]
data.sort
plt.hist(data)
plt.ylabel('Count')
plt.show()

The histogram it's OK but I don't like the grouping python uses to show grades in x-axis. I wonder if there is a way (some hist parameter or another plot library), where I can have bars for each individual grade?

Thanks in advance.

like image 864
NickP Avatar asked Aug 05 '26 01:08

NickP


1 Answers

It can be a little fussy, but you can customize almost everything once you have the right arguments. The main problems seem to be getting bins right and setting the base width with rwidth.

Maybe this is closer to what you are hoping for:

import matplotlib.pyplot as plt

plt.title('Histogram Grades')
data = [1, 3, 10, 8, 9, 5, 3, 7, 7, 3, 1, 3, 10, 8, 9, 5, 3, 7, 7, 3, 2]
plt.hist(data, rwidth=.8, bins=np.arange(min(data), max(data)+2) - 0.5)

plt.xticks(np.arange(min(data), max(data)+1, 1.0))
plt.ylabel('Count')
plt.show()

enter image description here

like image 153
Mark Avatar answered Aug 06 '26 13:08

Mark