Skip to content

Instantly share code, notes, and snippets.

@tommct
Last active January 10, 2018 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommct/7570f4ec2c1941dc1b954189095cbb31 to your computer and use it in GitHub Desktop.
Save tommct/7570f4ec2c1941dc1b954189095cbb31 to your computer and use it in GitHub Desktop.
Matplotlib normalized histograms

This creates a normalized mass density histogram in matplotlib

bins = np.linspace(-1, 1, 101)
# To get a normalized mass density histogram, we have to do it this way...
hist, bins = np.histogram(df['some_column'], bins=bins, density=True)
hist /= len(bins)
width = bins[1]-bins[0]
fig = plt.figure(figsize=(8, 4))
ax = fig.add_axes([.15, .15, .75, .75])
plt.bar(left=bins[:-1], height=hist, width=width)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment