{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook reproduces a [visualization by the Wall Street Journal](http://graphics.wsj.com/infectious-diseases-and-vaccines/#b02g20t20w15) about the incidence of measles over time, which the brilliant [Brian Granger](https://github.com/ellisonbg) adapted into an [example for the Altair library](http://nbviewer.jupyter.org/github/ellisonbg/altair/blob/master/altair/notebooks/12-Measles.ipynb).\n",
    "\n",
    "Most examples work across multiple plotting backends, this example is also available for:\n",
    "\n",
    "* [Matplotlib measles_example](../matplotlib/measles_example.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "import pandas as pd\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Declaring data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://raw.githubusercontent.com/blmoore/blogR/master/data/measles_incidence.csv'\n",
    "data = pd.read_csv(url, skiprows=2, na_values='-')\n",
    "\n",
    "yearly_data = data.drop('WEEK', axis=1).groupby('YEAR').sum().reset_index()\n",
    "measles = pd.melt(yearly_data, id_vars=['YEAR'], var_name='State', value_name='Incidence')\n",
    "\n",
    "heatmap = hv.HeatMap(measles, label='Measles Incidence')\n",
    "aggregate = hv.Dataset(heatmap).aggregate('YEAR', np.mean, np.std)\n",
    "\n",
    "vline = hv.VLine(1963)\n",
    "marker = hv.Text(1964, 800, 'Vaccine introduction', halign='left')\n",
    "\n",
    "agg = hv.ErrorBars(aggregate) * hv.Curve(aggregate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hm_opts = dict(width=900, height=500, tools=['hover'], logz=True, invert_yaxis=True,\n",
    "            xrotation=90, labelled=[], toolbar='above', xaxis=None)\n",
    "overlay_opts = dict(width=900, height=200, show_title=False)\n",
    "vline_opts = dict(line_color='black')\n",
    "\n",
    "opts = {'HeatMap': {'plot': hm_opts}, 'Overlay': {'plot': overlay_opts}, 'VLine': {'style': vline_opts}}\n",
    "\n",
    "(heatmap + agg * vline * marker).opts(opts).cols(1)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
