{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "URL: http://bokeh.pydata.org/en/latest/docs/gallery/histogram.html\n", "\n", "Most examples work across multiple plotting backends, this example is also available for:\n", "\n", "* [Bokeh - histogram_example](../bokeh/histogram_example.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipy\n", "import scipy.special\n", "import holoviews as hv\n", "from holoviews import opts\n", "\n", "hv.extension('matplotlib')\n", "hv.output(fig='svg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Declaring data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_overlay(hist, x, pdf, cdf, label):\n", " pdf = hv.Curve((x, pdf), label='PDF')\n", " cdf = hv.Curve((x, cdf), label='CDF')\n", " return (hv.Histogram(hist, vdims='P(r)') * pdf * cdf).relabel(label)\n", "\n", "\n", "label = \"Normal Distribution (μ=0, σ=0.5)\"\n", "mu, sigma = 0, 0.5\n", "\n", "measured = np.random.normal(mu, sigma, 1000)\n", "hist = np.histogram(measured, density=True, bins=50)\n", "\n", "x = np.linspace(-2, 2, 1000)\n", "pdf = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2 / (2*sigma**2))\n", "cdf = (1+scipy.special.erf((x-mu)/np.sqrt(2*sigma**2)))/2\n", "norm = get_overlay(hist, x, pdf, cdf, label)\n", "\n", "np.seterr(divide='ignore', invalid='ignore')\n", "\n", "label = \"Log Normal Distribution (μ=0, σ=0.5)\"\n", "mu, sigma = 0, 0.5\n", "\n", "measured = np.random.lognormal(mu, sigma, 1000)\n", "hist = np.histogram(measured, density=True, bins=50)\n", "\n", "x = np.linspace(0, 8.0, 1000)\n", "pdf = 1/(x* sigma * np.sqrt(2*np.pi)) * np.exp(-(np.log(x)-mu)**2 / (2*sigma**2))\n", "cdf = (1+scipy.special.erf((np.log(x)-mu)/(np.sqrt(2)*sigma)))/2\n", "lognorm = get_overlay(hist, x, pdf, cdf, label)\n", "\n", "\n", "label = \"Gamma Distribution (k=1, θ=2)\"\n", "k, theta = 1.0, 2.0\n", "\n", "measured = np.random.gamma(k, theta, 1000)\n", "hist = np.histogram(measured, density=True, bins=50)\n", "\n", "x = np.linspace(0, 20.0, 1000)\n", "pdf = x**(k-1) * np.exp(-x/theta) / (theta**k * scipy.special.gamma(k))\n", "cdf = scipy.special.gammainc(k, x/theta) / scipy.special.gamma(k)\n", "gamma = get_overlay(hist, x, pdf, cdf, label)\n", "\n", "\n", "label = \"Beta Distribution (α=2, β=2)\"\n", "alpha, beta = 2.0, 2.0\n", "\n", "measured = np.random.beta(alpha, beta, 1000)\n", "hist = np.histogram(measured, density=True, bins=50)\n", "\n", "x = np.linspace(0, 1, 1000)\n", "pdf = x**(alpha-1) * (1-x)**(beta-1) / scipy.special.beta(alpha, beta)\n", "cdf = scipy.special.btdtr(alpha, beta, x)\n", "beta = get_overlay(hist, x, pdf, cdf, label)\n", "\n", "\n", "label = \"Weibull Distribution (λ=1, k=1.25)\"\n", "lam, k = 1, 1.25\n", "\n", "measured = lam*(-np.log(np.random.uniform(0, 1, 1000)))**(1/k)\n", "hist = np.histogram(measured, density=True, bins=50)\n", "\n", "x = np.linspace(0, 8, 1000)\n", "pdf = (k/lam)*(x/lam)**(k-1) * np.exp(-(x/lam)**k)\n", "cdf = 1 - np.exp(-(x/lam)**k)\n", "weibull = get_overlay(hist, x, pdf, cdf, label)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(norm + lognorm + gamma + beta + weibull).opts(\n", " opts.Curve(axiswise=True),\n", " opts.Histogram(facecolor=\"#036564\", axiswise=True, bgcolor=\"#E8DDCB\"),\n", " opts.Layout(hspace=0.2)).cols(2)" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 2 }