2
$\begingroup$

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:

enter image description here

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.

$\endgroup$
  • $\begingroup$ I deleted my comment as it did the labeling correct, but didn't bin according to your specs... $\endgroup$ – kale Mar 17 '16 at 15:40
  • $\begingroup$ 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? $\endgroup$ – march Mar 17 '16 at 16:20
3
$\begingroup$

Quick n dirty:

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

enter image description here

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – mrz Mar 17 '16 at 16:45
  • $\begingroup$ seems like it would be hard to generalize. Maybe look at FindDivisions $\endgroup$ – george2079 Mar 17 '16 at 19:50
3
$\begingroup$

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}

enter image description here

$\endgroup$
  • $\begingroup$ Great ... but exactly ... see above, the bin around -1 (>-1.5 and <=-0.5) has 2 counts etc. $\endgroup$ – mrz Mar 17 '16 at 15:35
  • $\begingroup$ 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... $\endgroup$ – march Mar 17 '16 at 15:43
  • $\begingroup$ @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. $\endgroup$ – march Mar 17 '16 at 15:49
  • $\begingroup$ see Added ... but it works ... can you make it general? I put into your code BarSpacing -> None $\endgroup$ – mrz Mar 17 '16 at 16:05
  • $\begingroup$ @mrz. I'll work on it, but unfortunately I'm out of time for now! $\endgroup$ – march Mar 17 '16 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.