How to create a histogram / histogram with a bar at a discrete value?

I'm trying to create a histogram that displays the number of ratings for the value in the discrete values โ€‹โ€‹of the star (1-5). There must be a bar value by a value, and along the x axis the only numbers to be shown are [1,2,3,4,5] under each bar (in the center).

I tried setting the number of bins to 5 or their range from 0 to 7, but this creates stripes that span the values โ€‹โ€‹(as in the image below)

enter image description here

This is the code I 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") 
+6
source share
1 answer

You can use the plot(kind='bar') method:

 stars = Series(randint(1, 6, size=100)) vc = stars.value_counts().sort_index() ax = vc.plot(kind='bar') fig = ax.get_figure() fig.autofmt_xdate() 

To obtain:

enter image description here

EDIT # 1: To show them as proportions, simply divide by sum

 vc /= float(vc.sum()) assert vc.sum() == 1 

To obtain:

enter image description here

EDIT # 2: To show them as a percentage, separate them by the sum as above, and use the format specification mini-language to format the labels for the y axis labels

 new_labels = ['{0:.0%}'.format(float(x.get_text())) for x in ax.get_yticklabels()] ax.set_yticklabels(new_labels) 

To obtain:

enter image description here

+8
source

Source: https://habr.com/ru/post/952329/


All Articles