# How to 'center' a Histogram

How can a generate a histogram so the peak of the distribution is at x=0?

I want to center these histograms for comparison

(*k=2*)
Histogram@Table[Total@RandomInteger[1, {2}], {1000}]

(*k=4*)
Histogram@Table[Total@RandomInteger[1, {4}], {1000}]

(*k=10*)
Histogram@Table[Total@RandomInteger[1, {10}], {1000}]

(*k=50*)
Histogram@Table[Total@RandomInteger[1, {50}], {1000}]

(*k=100*)
Histogram@Table[Total@RandomInteger[1, {100}], {1000}]

(*k=10 000*)
Histogram@Table[Total@RandomInteger[1, {10000}], {1000}]

(*k=100 000*)
Histogram@Table[Total@RandomInteger[1, {100000}], {1000}]


edit: range was incorrect

edit 2: Got it! Histogram[RandomVariate[NormalDistribution[0, 1],k]]

• Are you sure Histogram is what you're after, vs say BarChart or a straight Plot? In addition, not clear what you mean by "Peak at x=0". A Histogram will have the "edge" of the "bars" at bin boundaries. You could massage the table results and bin specification to fake that (somewhat), but might be better to elucidate what you're trying to accomplish, might elicit a better solution... oops, you updated again as I was typing. If you answered your own question, consider posting it as such. – ciao Mar 10 '14 at 10:04
• I'm a new user so it won't let me post an answer. Sorry about that! – user12871 Mar 10 '14 at 10:35
• No worries, you'll get that soon, so if appropriate it's standard to self-answer. Read the doc's re: distribution functions, some cool ways you can accomplish what (I think) you're doing... – ciao Mar 10 '14 at 10:40

When wanting to do costum things with histograms, I find it's often best to use the functions HistogramDistribution and HistogramList. Which give you respectively a distribution object and a list defining the histogram. Below I've shown how i would compare the distributions, note that I'm using a line plot rather than the more appropriate boxchart since it makes it easier to compare the curves.

histogramMeanLine[{binEdges_, y_}] := {Mean /@ Partition[binEdges, 2, 1], y} // Transpose
centerPeak[v_] := {v[[1 ;;, 1]] - First@Last@SortBy[v, Last], v[[1 ;;, 2]]} // Transpose
myHist[v_] := ListLinePlot[centerPeak@histogramMeanLine@HistogramList@v, Filling->0,   PlotMarkers->Automatic]


Which then allow you do do the plot you wanted with:

SeedRandom
Table[myHist@Table[Total@RandomInteger[1, {n}], {1000}],
{n, {2,4,10,50,100,10000,100000}}] // Show

• jVincent, you seem to have stopped posting. I just wanted to let you know that it hasn't gone unnoticed and that I miss your contributions. I hope you soon find time and interest to continue your participation. – Mr.Wizard Jun 25 '14 at 13:15

You cannot centre a histogram: the histogram just represents the data. What you can "centre" is the data the histogram is built from.

Histogram[data]

Histogram[data - Mean[data]]