Histogram Example#

Download this notebook from GitHub (right-click to download).


URL: https://docs.bokeh.org/en/latest/docs/examples/topics/stats/histogram.html

Most examples work across multiple plotting backends, this example is also available for:

import numpy as np
import scipy
import scipy.special
import holoviews as hv
from holoviews import opts

hv.extension('matplotlib')
hv.output(fig='svg')

Declaring data#

def get_overlay(hist, x, pdf, cdf, label):
    pdf = hv.Curve((x, pdf), label='PDF')
    cdf = hv.Curve((x, cdf), label='CDF')
    return (hv.Histogram(hist, vdims='P(r)') * pdf * cdf).relabel(label)


label = "Normal Distribution (μ=0, σ=0.5)"
mu, sigma = 0, 0.5

measured = np.random.normal(mu, sigma, 1000)
hist = np.histogram(measured, density=True, bins=50)

x = np.linspace(-2, 2, 1000)
pdf = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2 / (2*sigma**2))
cdf = (1+scipy.special.erf((x-mu)/np.sqrt(2*sigma**2)))/2
norm = get_overlay(hist, x, pdf, cdf, label)

np.seterr(divide='ignore', invalid='ignore')

label = "Log Normal Distribution (μ=0, σ=0.5)"
mu, sigma = 0, 0.5

measured = np.random.lognormal(mu, sigma, 1000)
hist = np.histogram(measured, density=True, bins=50)

x = np.linspace(0, 8.0, 1000)
pdf = 1/(x* sigma * np.sqrt(2*np.pi)) * np.exp(-(np.log(x)-mu)**2 / (2*sigma**2))
cdf = (1+scipy.special.erf((np.log(x)-mu)/(np.sqrt(2)*sigma)))/2
lognorm = get_overlay(hist, x, pdf, cdf, label)


label = "Gamma Distribution (k=1, θ=2)"
k, theta = 1.0, 2.0

measured = np.random.gamma(k, theta, 1000)
hist = np.histogram(measured, density=True, bins=50)

x = np.linspace(0, 20.0, 1000)
pdf = x**(k-1) * np.exp(-x/theta) / (theta**k * scipy.special.gamma(k))
cdf = scipy.special.gammainc(k, x/theta) / scipy.special.gamma(k)
gamma = get_overlay(hist, x, pdf, cdf, label)


label = "Beta Distribution (α=2, β=2)"
alpha, beta = 2.0, 2.0

measured = np.random.beta(alpha, beta, 1000)
hist = np.histogram(measured, density=True, bins=50)

x = np.linspace(0, 1, 1000)
pdf = x**(alpha-1) * (1-x)**(beta-1) / scipy.special.beta(alpha, beta)
cdf = scipy.special.btdtr(alpha, beta, x)
beta = get_overlay(hist, x, pdf, cdf, label)


label = "Weibull Distribution (λ=1, k=1.25)"
lam, k = 1, 1.25

measured = lam*(-np.log(np.random.uniform(0, 1, 1000)))**(1/k)
hist = np.histogram(measured, density=True, bins=50)

x = np.linspace(0, 8, 1000)
pdf = (k/lam)*(x/lam)**(k-1) * np.exp(-(x/lam)**k)
cdf = 1 - np.exp(-(x/lam)**k)
weibull = get_overlay(hist, x, pdf, cdf, label)

Plot#

(norm + lognorm + gamma + beta + weibull).opts(
    opts.Curve(axiswise=True),
    opts.Histogram(facecolor="#036564", axiswise=True,  bgcolor="#E8DDCB"),
    opts.Layout(hspace=0.2)).cols(2)
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).