Thursday, 22 August 2013

How to create a bar chart/histogram with bar per discrete value?

How to create a bar chart/histogram with bar per discrete value?

I am trying to create a histogram that will show the amount of ratings per
value in a discrete star ratings (1-5). There should be a bar per value,
and in the x-axis the only numbers to be shown are [1,2,3,4,5] underneath
each bar (centered).
I tried setting the amount of bins to 5 or their range to be from 0-7, but
that creates bars that span across values (as in the image supplied)

This is the code I have tried (pandas and numpy):
df.stars.hist()
and
hist, bins = np.histogram(x1, bins=5)
ax.bar(bins[:-1], hist.astype(np.float32) / hist.sum(),
width=(bins[1]-bins[0]), color="blue")

No comments:

Post a Comment