# Are there any good general techniques for binning/histogramming arbitrary data?

Let's say we want to histogram a finite set of measurements of some quantity. It is straight forward to calculate the usual statistical quantities for our sample such as the mean and the variance. Let's assume we can clean up our data by identifying outliers and moving them into underflow and overflow bins and thus, define more or less optimal min and max values for the plotting range. But how would one decide on the number and the size of bins? I would like to know if there are methods to find the optimal binning for the cases with fixed and variable bin sizes.

## 1 Answer

I don't know if this what you want but here is a way to calculate the number of bins.

1. Count the number of data points in your dataset.
2. Take the square root of the number of data points and round up to determine the initial number of bins required: $$InitialNumberOfBins = \sqrt{NumberOfDataPoints}$$.
3. Divide the specification tolerance $$Max-Min\ value$$ by initial number of bins: $$FinalNumberOfBins = (Max-Min\ value) / InitialNumberOfBins$$.
• (1 - 0) / sqrt(10000) = 0.01 bins?? How does that work? – K3---rnc Jun 18 '16 at 13:22
• you square root the number of data points i.e (√144)= 12 initial bins – Darrin Thomas Jun 18 '16 at 13:36
• Yes, 10k data points, normalized to [0, 1], squared, as above. Gives 0.01 bins. – K3---rnc Jun 18 '16 at 15:57
• I don't remember saying to normalize the data. – Darrin Thomas Jun 18 '16 at 21:10
• Sorry, it was already normalized. But I just used it as an example. The real data has values between 14.03 and 14.93, roughly normally distributed. So? – K3---rnc Jun 19 '16 at 17:05