# Creating histograms with specified mean and intervals

I have a set of data that I want to create a histogram for. The thing is, I would like to set the center of the histogram to the mean of the dataset and the intervals to be the standard deviation. How would I go about specifying this in Mathematica?

Just for the sake of it, this is my dataset and all I have in Mathematica so far:

Histogram[{0, 0, 0, 0, 80, 100, 120, 130, 130, 140, 140, 150, 150, 160, 170, 200, 220, 240, 350}]


Perhaps:

data = {0, 0, 0, 0, 80, 100, 120, 130, 130, 140, 140, 150, 150, 160, 170, 200, 220, 240, 350};
{mean, std} = Through@{Mean, StandardDeviation}@data;


You can specify the bin delimiters as an explicit list:

bins = {Table[mean + k std, {k , -5/2, 5/2, 1}]};

Histogram[data, bins, Epilog -> {PointSize[Large], Red, Point@{mean, 0}}] Alternatively, you can use the form {xmin,xmax, dx} for bin specification:

bins2 = {mean - 5/2 std, mean + 5/2 std, std};
Histogram[data, bins2, Epilog -> {PointSize[Large], Red, Point@{mean, 0}}]
(* same picture *)


You can use this binning function to avoid having to recalculate the mean and standard deviation if the data changes, or manually decide on how many standard deviations to include.

binFunction[l_List] :=
Module[{μ = Mean[l], σ = StandardDeviation[l]},
Mean[l] - σ/2 + σ Range[Floor[Min[(l - μ)/σ]],
Ceiling[Max[(l - μ)/σ]]]]


Example of usage:

data = RandomVariate[StudentTDistribution[Pi], 1000];
Histogram[data, binFunction, Epilog -> {Red, Point[{Mean[data], 0}]}]


If you always want the plot to have equal range on either side of the mean, regardless of the distribution shape, use this function instead:

binFunctionSymmetric[l_List] :=
Module[{μ = Mean[l], σ = StandardDeviation[l]},
Mean[l] - σ/2 + σ Range[-#, +#] &@
Ceiling[Max[Abs[(l - μ)/σ]]]]

• I think this solution can be made identical to the first one by just sliding the range in binFunction by σ/2. In fact, I edited the solution to that effect. – FACamargo Sep 23 '16 at 11:00
• @FACamargo To me it's not clear from the question which alignment is preferred (personally I'd prefer the bin divisions to line up with the standard deviations); but, seeing the accepted answer it looks like you're right, thanks for the edit! – 2012rcampion Sep 23 '16 at 12:26