Measles example

Download this notebook from GitHub (right-click to download).


This notebook reproduces a visualization by the Wall Street Journal about the incidence of measles over time, which the brilliant Brian Granger adapted into an example for the Altair library.

Most examples work across multiple plotting backends, this example is also available for:

In [1]:
import numpy as np
import holoviews as hv
import pandas as pd
from holoviews import opts

hv.extension('matplotlib')
hv.output(fig='svg')

Declaring data

In [2]:
url = 'https://raw.githubusercontent.com/blmoore/blogR/master/data/measles_incidence.csv'
data = pd.read_csv(url, skiprows=2, na_values='-')

yearly_data = data.drop('WEEK', axis=1).groupby('YEAR').sum().reset_index()
measles = pd.melt(yearly_data, id_vars=['YEAR'], var_name='State', value_name='Incidence')

heatmap = hv.HeatMap(measles, label='Measles Incidence')
aggregate = hv.Dataset(heatmap).aggregate('YEAR', np.mean, np.std)

vline = hv.VLine(1963)
marker = hv.Text(1964, 800, 'Vaccine introduction', halign='left')

agg = hv.ErrorBars(aggregate) * hv.Curve(aggregate)

Plot

In [3]:
(heatmap + agg * vline * marker).opts(
    opts.HeatMap(aspect=1.4,invert_yaxis=True, show_values=False, show_frame=False,
                 labelled=[], xaxis=None, logz=True),
    opts.Layout(aspect_weight=1, fig_size=300, vspace=0, sublabel_format=None),
    opts.Overlay(aspect=3, show_title=False, bgcolor='white', 
                 show_legend=False, show_frame=False, xrotation=90),
    opts.VLine(color='black', ylim=(0, 1050))).cols(1)
Out[3]:

Download this notebook from GitHub (right-click to download).