Skip to content

Instantly share code, notes, and snippets.

@colobas
Last active November 23, 2018 17:58
Show Gist options
  • Save colobas/c06e1bb0bcdef554067f6f9cc6f9bc8f to your computer and use it in GitHub Desktop.
Save colobas/c06e1bb0bcdef554067f6f9cc6f9bc8f to your computer and use it in GitHub Desktop.
Compute the recommended bin size for a histogram, according to the Freedman-Diaconis rule
def bin_size(x):
"""
https://en.wikipedia.org/wiki/Freedman%E2%80%93Diaconis_rule
"""
return (2 * (np.nanquantile(x, 0.75) - np.nanquantile(x, 0.25)) /
np.cbrt(len(x[~np.isnan(x)])))
@sfrodrigues
Copy link

the quantiles are in the wrong order :)

@colobas
Copy link
Author

colobas commented Nov 23, 2018

good point! @sfrodrigues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment