{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **Title**: Histogram Element\n",
    "\n",
    "**Dependencies**: Plotly\n",
    "\n",
    "**Backends**: [Bokeh](../bokeh/Histogram.ipynb), [Matplotlib](../matplotlib/Histogram.ipynb), [Plotly](./Histogram.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import opts\n",
    "hv.extension('plotly')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "``Histogram``s partition the `x` axis into discrete (but not necessarily regular) bins, showing counts in each as a bar. A ``Histogram`` accepts the output of ``np.histogram`` as input, which consists of a tuple of the histogram values with a shape of ``N`` and bin edges with a shape of ``N+1``. As a simple example we will generate a histogram of a normal distribution with 20 bins."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(1)\n",
    "data = np.random.randn(10000)\n",
    "frequencies, edges = np.histogram(data, 20)\n",
    "print('Values: %s, Edges: %s' % (frequencies.shape[0], edges.shape[0]))\n",
    "hv.Histogram((edges, frequencies))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``Histogram`` Element will also expand evenly sampled bin centers, therefore we can easily cast between a linearly sampled ``Curve`` or ``Scatter`` and a ``Histogram``."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xs = np.linspace(0, np.pi*2)\n",
    "ys = np.sin(xs)\n",
    "curve = hv.Curve((xs, ys))\n",
    "curve + hv.Histogram(curve)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Like most other elements a ``Histogram`` also supports using ``dim`` transforms to map dimensions to visual attributes. To demonstrate this we will use the ``bin`` op to bin the 'y' values into positive and negative values and map those to a 'blue' and 'red' ``fill_color``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.Histogram(curve).opts(\n",
    "    opts.Histogram(color=hv.dim('y').bin(bins=[-1, 0, 1], labels=['red', 'blue'])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``.hist`` method is an easy way to compute a histogram from an existing Element:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "points = hv.Points(np.random.randn(100,2))\n",
    "points.hist(dimension=['x','y'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``.hist`` method is just a convenient wrapper around the ``histogram`` operation that computes a histogram from an Element, and then adjoins the resulting histogram to the main plot. You can also do this process manually; here we create an additional set of ``Points``, compute a ``Histogram`` for the 'x' and 'y' dimension on each, and then overlay them and adjoin to the plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from holoviews.operation import histogram\n",
    "points2 = hv.Points(np.random.randn(100,2)*2+1)\n",
    "\n",
    "xhist, yhist = (histogram(points2, bin_range=(-5, 5), dimension=dim) *\n",
    "                histogram(points,  bin_range=(-5, 5), dimension=dim) \n",
    "                for dim in 'xy')\n",
    "\n",
    "composition = (points2 * points) << yhist.opts(width=125) << xhist.opts(height=125)\n",
    "composition.opts(opts.Histogram(opacity=0.3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For full documentation and the available style and plot options, use ``hv.help(hv.Histogram).``"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
