Choosing the number of bins in a histogram has always been something that gets me thinking a lot. Based on the number of bins chosen, the graph at time looks a lot different and also could lead to different interpretations.

Below is the square-root rule, which I use as the thumb-rule for selecting the number of bins in most occasions.

Posting this question here to hear other opinions.

data_pts = len(np.array(data))
bin_cnt = int(np.sqrt(data_pts))

plt.hist(data, bins=bin_cnt)
  • 1
    $\begingroup$ This might help: stats.stackexchange.com/questions/798/… Also there's this Sturges rule whose formula is similar to your code. $\endgroup$ Jan 24 '20 at 0:07
  • 1
    $\begingroup$ Thanks @FatemehAsgarinejad, that answer does cover a number of methods. $\endgroup$
    – loki
    Jan 24 '20 at 5:16

Another method is Bayesian Blocks from Studies in Astronomical Time Series Analysis. VI. Bayesian Block Representations by Scargle et al.

Bayesian Blocks is a dynamic histogramming method which optimizes one of several possible fitness functions to determine an optimal binning for data, where the bins are not necessarily uniform width.

Bayesian Blocks for Histograms


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.