# Histogram distribution

Imagine the following data:

data = {-0.7, -0.5, -0.2, 0.1, 0.4, 0.5, 0.9, 1.1, 1.2, 1.3, 1.5, 2.1}


From that I make a Histogram:

Histogram[data, {1}]


The output plot is:

I would like another way of binning the data:

The bins should be symmetric around the integer numbers:

]-1.5,-0.5]: 2 counts
]-0.5,+0.5]: 4 counts
]+0.5,+1.5]: 5 counts
]+1.5,+2.5]: 1 count


Added: It should work also for other bin width's:

e.g. for bin width = 2 the intervals are ...]-3,-1], ]-1,+1], ]+1,+3], etc ...

How can that be done? The number and tick marks should be centered below the bins.

I saw in many examples that the normal binning is used and that only the tick marks and position of the numbers at the x axis were shifted, which is here not the same.

• I deleted my comment as it did the labeling correct, but didn't bin according to your specs... – kale Mar 17 '16 at 15:40
• What is the general criterion for the bin center? For your Added example, how do you choose whether to center the bins at even integers rather than odd integers? – march Mar 17 '16 at 16:20

Quick n dirty:

Histogram[data - 10^-10, {-1.5, 2.5, 1}]


• Great ... for bin size=0.5 I would use Histogram[data - 10^-10, {-1.25, 2.25, 0.5}], which is correct, and for bin size=2 Histogram[data - 10^-10, {-3, 3, 2}]. Can that procedure be expressed as function of bin size? – mrz Mar 17 '16 at 16:45
• seems like it would be hard to generalize. Maybe look at FindDivisions – george2079 Mar 17 '16 at 19:50

What about something like this? It's not well-tested, so I can't guarantee that the bin labels will always line up with the correct bin, but that can be tweaked pretty easily.

data = {-0.7, -0.5, -0.2, 0.1, 0.4, 0.5, 0.9, 1.1, 1.2, 1.3, 1.5, 2.1};
BarChart[
Reverse@BinCounts[-data, {-#2 + 0.5, -#1 + 0.5, 1}],
ChartLabels -> Range @@ {#1, #2}
] & @@ {Floor@Min@data, Ceiling@Max@data}


• Great ... but exactly ... see above, the bin around -1 (>-1.5 and <=-0.5) has 2 counts etc. – mrz Mar 17 '16 at 15:35
• I see: both Histogram and BinCounts have the closed end of the interval on the left, and you want it on the right. I think I can figure that out... – march Mar 17 '16 at 15:43
• @mrz. Does that work? Again, I'm not sure if it's general, and it seems like there should be some option for Histogram that makes it work, but I can't find it yet. – march Mar 17 '16 at 15:49
• see Added ... but it works ... can you make it general? I put into your code BarSpacing -> None – mrz Mar 17 '16 at 16:05
• @mrz. I'll work on it, but unfortunately I'm out of time for now! – march Mar 17 '16 at 16:11