Histogram example¶
Download this notebook from GitHub (right-click to download).
URL: http://bokeh.pydata.org/en/latest/docs/gallery/histogram.html
Most examples work across multiple plotting backends, this example is also available for:
In [1]:
import numpy as np
import scipy
import scipy.special
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
Declaring data¶
In [2]:
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)
np.seterr(divide='ignore', invalid='ignore')
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)
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¶
In [3]:
layout = (norm + lognorm + gamma + beta + weibull).cols(2)
layout.opts(
opts.Curve(axiswise=True),
opts.Histogram(fill_color="#036564", axiswise=True, height=350, width=350, bgcolor="#E8DDCB"),
opts.Layout(shared_axes=False))
Out[3]:
Download this notebook from GitHub (right-click to download).