!date
import numpy as np, pandas as pd, matplotlib.pyplot as plt, mpld3, seaborn as sns
%matplotlib inline
df = pd.read_csv('http://ghdx.healthdata.org/sites/default/files/record-attached-files/'
'IHME_GBD_2010_MORTALITY_AGE_SPECIFIC_BY_COUNTRY_1970_2010.CSV')
df.head()
# select data for a specific country
df = df[df.iso3=='ZAF']
# and for a specific sex
df = df[df.sex_name=='Male']
# and plot deaths over time by age group
fig, ax = plt.subplots(figsize=(12,8))
labels = []
line_collections = []
for g, dfg in df.groupby('age_name'):
if g == 'All ages':
continue
x = dfg.year
y = dfg.death_abs.map(lambda x: float(x.replace(',', '')))
l, = ax.plot(x, y, 'o-', lw=4, ms=15)
labels.append(g)
line_collections.append(l)
pt_labels = ['Age %s
Year %s
%d Deaths'%(g, x.iloc[i], y.iloc[i]) for i in range(len(x))]
tooltip = mpld3.plugins.PointHTMLTooltip(l, labels=pt_labels)
mpld3.plugins.connect(fig, tooltip)
plt.axis(xmin=1968, xmax=2012)
plt.xticks([1970, 1980, 1990, 2000, 2010], [1970, 1980, 1990, 2000, 2010])
plt.subplots_adjust(right=.7)
interactive_legend = mpld3.plugins.InteractiveLegendPlugin(line_collections, labels, alpha_sel=.2, alpha_unsel=1)
mpld3.plugins.connect(fig, interactive_legend)
mpld3.display()