{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **Title**: Bounds & selection stream example\n",
    "\n",
    "**Description**: A linked streams example demonstrating how to use Bounds and Selection streams together.\n",
    "\n",
    "**Dependencies** Bokeh\n",
    "\n",
    "**Backends** [Bokeh](./Bounds.ipynb), [Plotly](../plotly/Bounds.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",
    "from holoviews import streams\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "opts.defaults(opts.Histogram(framewise=True))\n",
    "\n",
    "# Declare distribution of Points\n",
    "points = hv.Points(np.random.multivariate_normal((0, 0), [[1, 0.1], [0.1, 1]], (1000,)))\n",
    "\n",
    "# Declare points selection selection\n",
    "sel = streams.Selection1D(source=points)\n",
    "\n",
    "# Declare DynamicMap computing mean y-value of selection\n",
    "mean_sel = hv.DynamicMap(lambda index: hv.HLine(points['y'][index].mean() if index else -10),\n",
    "                         kdims=[], streams=[sel])\n",
    "\n",
    "# Declare a Bounds stream and DynamicMap to get box_select geometry and draw it\n",
    "box = streams.BoundsXY(source=points, bounds=(0,0,0,0))\n",
    "bounds = hv.DynamicMap(lambda bounds: hv.Bounds(bounds), streams=[box])\n",
    "\n",
    "# Declare DynamicMap to apply bounds selection\n",
    "dmap = hv.DynamicMap(lambda bounds: points.select(x=(bounds[0], bounds[2]),\n",
    "                                                  y=(bounds[1], bounds[3])),\n",
    "                     streams=[box])\n",
    "\n",
    "# Compute histograms of selection along x-axis and y-axis\n",
    "yhist = hv.operation.histogram(dmap, bin_range=points.range('y'), dimension='y', dynamic=True, normed=False)\n",
    "xhist = hv.operation.histogram(dmap, bin_range=points.range('x'), dimension='x', dynamic=True, normed=False)\n",
    "\n",
    "# Combine components and display\n",
    "points * mean_sel * bounds << yhist << xhist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"https://assets.holoviews.org/gifs/examples/streams/bokeh/bounds_selection.gif\" width=400></center>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
