In [14]:
%matplotlib inline
sns.set_style('white')
sns.set_context('poster')
cmap = sns.diverging_palette(220, 10, as_cmap=True)
In [15]:
# Get registration application data
res = requests.get('https://www.performance.service.gov.uk/data/register-to-vote/volumetrics?duration=30&collect=count%3Asum&group_by=value&period=day&filter_by=metricName%3Aage_band')
# res.json()['data']
In [16]:
def munge_reg_apps(res):
""" Transorm JSON into DataFrame-compabible format. """
apps = DataFrame()
data = res.json()['data']
for age_group in data:
age_df = DataFrame(age_group['values'])
age_df['age_group'] = age_group['value']
apps = apps.append(age_df)
apps[['_end_at', '_start_at']] = apps[['_end_at', '_start_at']].apply(pd.to_datetime)
apps = apps.rename(columns={'_end_at': 'end_date', '_start_at': 'start_date', 'count:sum': 'total'})
apps = apps.drop(labels=['_count'], axis=1)
apps['age_group'] = apps.age_group.astype('category')
return apps.reset_index(drop=True)
In [17]:
apps = munge_reg_apps(res)
apps.head()
Out[17]:
In [18]:
apps.groupby('age_group').sum()
Out[18]:
In [19]:
apps.groupby('age_group').sum().plot(kind='bar', title='Registration applications by age group');
In [20]:
apps.groupby('age_group')[['total']].describe().unstack()
Out[20]:
In [21]:
age_apps = apps.pivot(index='start_date', columns='age_group', values='total')
age_apps
Out[21]:
In [22]:
# Breaks plot otherwise :(
age_apps.index = age_apps.index.astype('str')
In [23]:
age_apps.plot(title='Registration applications by age over time');
In [24]:
age_apps.cumsum().plot(title='Cumulative applications by age over time');
In [25]:
ax = sns.heatmap(
age_apps.drop(labels=['14-15', '16-17', 'not_provided'], axis=1).apply(lambda s: np.log(s)).T,
cmap=cmap,
square=True,
linewidths=2,
cbar_kws={'shrink': 0.4},
)
ax.set_title('Log applications by age over time');
In [26]:
ax = sns.heatmap(
age_apps.cumsum().drop(labels=['14-15', '16-17', 'not_provided'], axis=1).T,
cmap=cmap,
square=True,
linewidths=2,
cbar_kws={'shrink': 0.4},
)
ax.set_title('Cumulative applications by age over time');
In [ ]: