Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

enter image description here

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")
like image 341
d1337 Avatar asked Aug 23 '13 02:08

d1337


People also ask

Can histograms use discrete data?

The histogram is a popular graphing tool. It is used to summarize discrete or continuous data that are measured on an interval scale. It is often used to illustrate the major features of the distribution of the data in a convenient form.

Do you use a bar graph for discrete variables?

Bar charts should be used when you are showing segments of information. Vertical bar charts are useful to compare different categorical or discrete variables, such as age groups, classes, schools, etc., as long as there are not too many categories to compare.

Is a bar chart best for discrete numerical data?

2.4 Bar Chart. If your data isn't continuous you have other options, and generally discrete numerical data or categorical data (either nominal or ordinal) can be graphed in the same way. With categorical or discrete data a bar chart is typically your best option.


1 Answers

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 get:

enter image description here

EDIT #1: To show them as proportions just divide by the sum

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

to get:

enter image description here

EDIT #2: To show them as percentages divide by the sum as above and use the format spec mini-language to format the y-axis tick labels

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

to get:

enter image description here

like image 170
Phillip Cloud Avatar answered Oct 23 '22 01:10

Phillip Cloud