Skip to content

Commit

Permalink
Add cartogram option for maps
Browse files Browse the repository at this point in the history
sebbacon committed Mar 5, 2019
1 parent cf3d3cf commit e4c9592
Showing 4 changed files with 265 additions and 10 deletions.
202 changes: 202 additions & 0 deletions ebmdatalab/data/ccgs_cartogram.json

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions ebmdatalab/maps.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
from pathlib import Path


def ccg_map(df, title="", column=None, separate_london=True):
def ccg_map(df, title="", column=None, separate_london=False, cartogram=False):
"""Draw a CCG map with London separated out
"""
assert column, "You must specify a column name to plot"
@@ -20,9 +20,17 @@ def ccg_map(df, title="", column=None, separate_london=True):
df['name'] = df["name"].str.replace("&","AND")
df = df.set_index('name')

ccgs = gpd.read_file(data_dir / 'ccgs.json').set_index('name')
if cartogram:
ccgs = gpd.read_file(data_dir / 'ccgs_cartogram.json')
else:
ccgs = gpd.read_file(data_dir / 'ccgs.json')

# Normalise because the two GeoJSON files have a different format
ccgs['name'] = ccgs['name'].str.upper()
ccgs = ccgs.set_index('name')

# remove ones without geometry - these are federations rather than individual CCGs
# remove ones without geometry - these are federations rather than
# individual CCGs
ccgs = ccgs[~ccgs['geometry'].isnull()]
gdf = ccgs.join(df)

51 changes: 46 additions & 5 deletions examples/charts.ipynb

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions examples/charts.py
Original file line number Diff line number Diff line change
@@ -21,18 +21,22 @@
#
# Draw a CCG map, optionally with London separated out.

# +
# + {"scrolled": false}
import numpy as np
import pandas as pd
from ebmdatalab import maps


df = pd.read_json('ccg_list_size.json')
df.columns = ['date', 'pct', 'ccg_name', 'total_list_size'] # The CCG column must be named 'pct'
plt = maps.ccg_map(df, title="CCG list sizes", column='total_list_size', separate_london=True)
plt.show()
# -

# You can also show the map as a cartogram where the CCGs are sized according to their patient population

plt = maps.ccg_map(df, title="CCG list sizes", column='total_list_size', cartogram=True, separate_london=False)
plt.show()

# ## Deciles
#
# Given a dataframe with a date column and a values column, compute

0 comments on commit e4c9592

Please sign in to comment.