{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
    "    <dl class=\"dl-horizontal\">\n",
    "      <dt>Title</dt> <dd> HeatMap Tap stream example</dd>\n",
    "      <dt>Description</dt> <dd>A linked streams example demonstrating how use Tap stream on a HeatMap. The data contains the incidence of measles across US states by year and week (obtained from [Project Tycho](http://www.tycho.pitt.edu/)). The HeatMap represents the mean measles incidence per year. On tap the Histogram on the right will generate a Histogram of the incidences for each week in the selected year and state.</dd>\n",
    "      <dt>Backends</dt> <dd> Bokeh</dd>\n",
    "      <dt>Tags</dt> <dd> streams, tap, interactive</dd>\n",
    "    </dl>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import streams\n",
    "hv.extension('bokeh', width=90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%opts HeatMap [width=700 height=500 logz=True fontsize={'xticks': '6pt'}, tools=['hover'] xrotation=90] (cmap='RdBu_r') \n",
    "%opts Curve [width=375 height=500 yaxis='right'] (line_color='black') {+framewise}\n",
    "\n",
    "# Declare dataset\n",
    "df = pd.read_csv('http://assets.holoviews.org/data/diseases.csv.gz', compression='gzip')\n",
    "dataset = hv.Dataset(df, vdims=('measles','Measles Incidence'))\n",
    "\n",
    "# Declare HeatMap\n",
    "heatmap = hv.HeatMap(dataset.aggregate(['Year', 'State'], np.mean),\n",
    "                     label='Measles Incidence').select(Year=(1928, 2002))\n",
    "\n",
    "# Declare Tap stream with heatmap as source and initial values\n",
    "posxy = hv.streams.Tap(source=heatmap, x=1951, y='New York')\n",
    "\n",
    "# Define function to compute histogram based on tap location\n",
    "def tap_histogram(x, y):\n",
    "    return hv.Curve(dataset.select(State=y, Year=int(x)), kdims='Week',\n",
    "                   label='Year: %s, State: %s' % (x, y))\n",
    "\n",
    "heatmap + hv.DynamicMap(tap_histogram, kdims=[], streams=[posxy])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"https://assets.holoviews.org/gifs/examples/streams/bokeh/heatmap_tap.gif\" width=600></center>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
