{ "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": [ "options = {'HeatMap': dict(width=900, height=500, tools=['hover'], logz=True, invert_yaxis=True,\n", " labelled=[], toolbar='above', xaxis=None), \n", " 'Overlay': dict(width=900, height=200, show_title=False, xrotation=90), \n", " 'VLine': dict(line_color='black')}\n", "\n", "(heatmap + agg * vline * marker).options(options).cols(1)" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 1 }