# How do I create a histogram with unequal bin widths?

I would like to create a histogram for human population by age, but the $2010$ census data I have available is for five uneven cohorts: ages $0$-$17$, $18$-$24$, $25$-$44$, $45$-$64$, and $65+$. My county has the following data: {86031, 26671, 91927, 93983, 32232}, for a total population of $330844$. The corresponding histogram bins should have widths {17, 7, 20, 20, 20} and heights {86031/17, 26671/7, 91927/20, 93983/20, 32232/20}. I will eventually want to make this pretty by adding labels, etc., but I don't see how to even get started.

• The second argument of the Histogram function directly specifies the bins to use. Histogram[{x1,x2},bspec] plots a histogram with bin width specification bspec. Nov 8 '13 at 4:14
• Nov 8 '13 at 7:15

Based solely on the information provided IMO you need to use RectangleChart. I'm not sure how to create the histogram you desire using Histogram and only the information in your question

-- here is the RectangleChart implementation:

{widths, totals} = {{17, 7, 20, 20, 20}, {86031, 26671, 91927, 93983,
32232}};
RectangleChart[Transpose@{widths, totals/widths}]


You can make this pretty:

RectangleChart[Transpose@{widths, totals/widths},
BarSpacing -> 2,
ChartBaseStyle -> EdgeForm[Dashed],
ChartLabels -> {"0-17", "18-24", "25-44", "45-64", "65+"},
ChartStyle -> ColorData["Charting"],
BaseStyle -> Directive[FontFamily -> "Arial", 12],
PerformanceGoal -> "Speed"]


The natural way is to use RectangleChart since your data is already processed.

I will just show how to use Histogram for your purposes, it requires step back to create the data, that's why Mike's solution is better.

data = {{{0, 17}, {17, 24}, {24, 44}, {44, 64}, {64, 84}},
{86031, 26671, 91927, 93983, 32232}};

dat = RandomReal[#, Round[#2/Abs[Subtract @@ #]]] & @@@ Transpose[data];

Histogram[dat, {{0, 17, 24, 44, 64, 84}}]


• I guess thats what I meant in my answer. The data presented in the question is already binned/aggregated so Histogram is the wrong choice in this instance unless you go backwards first. Nov 8 '13 at 7:55
• The RectangleChart approach Mike Honeychurch proposes is certainly more in keeping with my Mathematica skills. Kuba's solution — apparently by generating random data in the proper bins in the proper amounts to create the desired histogram — is awesome, but beyond my ability! Nov 9 '13 at 17:03

Alternatively you can fix both the bin end points and their heights explicitly. I've set 120 as a reasonable endpoint for the last bin. The zero in the input data is just there as a place holder and is effectively ignored.

Histogram[{0}, {{0, 18, 25, 45, 65, 120}}, {86031/17, 26671/7,
91927/20, 93983/20, 32232/20} &]


I would use the rectangle chart approach as others have suggested but this at least gives a way to use the height function to do what you are wanting without resorting to sampling.

Obviously if you want to label the axis differently you can use Ticks.

Histogram[{0}, {{0, 18, 25, 45, 65, 120}}, {86031/17, 26671/7,
91927/20, 93983/20, 32232/20} &,
Ticks -> {{{(0 + 17)/2, "0-17"}, {(18 + 24)/2, "18-24"}, {(25 + 44)/
2, "25-44"}, {(45 + 64)/2, "45-64"}, {(65 + 120)/2, "65+"}}, Automatic}]