shawnKemp
shawnKemp

Reputation: 31

The bin count displayed by pyplot hist doesn't match the actual counts?

I have a list of ints--I call it 'hours1'--ranging from 0-23. Now this list is for 'hours' of a day in a 24 hour clock. I, however, want to transform it to a different time zone (move up 7 hours). This is simple enough, and I do it so that now I have 2 lists: hours1 and hours2.

I use the following code to plot a histogram:

bins = range(24)
plt.hist(hours,bins=bins, normed=0, facecolor='red', alpha=0.5)
plt.axis([0, 23, 0, 1000])

it works perfectly for hours1. For hours2 the last value (that of the bin for 23s) is too high. This is not a counting error/transformation error because when I count my hours2 list, I get 604 23s, which matches the what I expect (having 604 16s in hours1).

so this is a very long winded way of saying, the height of the bins do not match what I get when I count the data myself...

Upvotes: 2

Views: 984

Answers (1)

shawnKemp
shawnKemp

Reputation: 31

The issue was a binning one. In short, I wasn't paying attention/thinking about what I wanted to display. More specifically this was the correct code:

bins = range(25)
plt.hist(hours, normed=0, facecolor='green', alpha=0.5, bins=bins)
plt.axis([0, 24, 0, 1500])

that is, there are 23 hours in a day, which means 24 seperate 'hour bins' counting 0. but the correct edge values for this are bins = range(25) (so that 23 gets placed in 23-24) and the correct axis is 0 to 24, (so bin 23 has width of 1). simple mistake, but i guess we've all bin there and done that?

Upvotes: 1

Related Questions