In this post I wanted to give a simple example of plotting country level data. This uses the ONS data sets on boundaries. First let's download the data and pop it in a directory:
In [10]:
%%bash
cd ../data_sets/
wget http://census.edina.ac.uk/ukborders/easy_download/prebuilt/shape/England_ct_2011.zip -O zipdata.zip -q
unzip -o zipdata.zip
rm zipdata.zip
Check that it is there!
In [13]:
ls ../data_sets/
Note that only some of these files are needed, but I don't know which so we keep them all.
Now we get to buisness. First import the normal things in addition to: shapefile which is alias of the module pyshp.
In [14]:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
import shapefile as sf
import glob
Now we will use the shapefile module to read in the shapes and metadata
In [15]:
shpFilePath = glob.glob("../data_sets/*shp")[0]
map_f = sf.Reader(shpFilePath)
county_metadata = map_f.records()
county_shapes = map_f.shapes()
Each country has a name:
In [16]:
county_metadata[0]
Out[16]:
and the shape file has a number of attributes:
In [17]:
dir(county_shapes[0])
Out[17]:
Firstly we could simply plot the points for each country drawing the relevant boundries (the idea was stolen from this lovely post)[http://assorted-experience.blogspot.co.uk/2013/10/plotting-state-boundary-data-from.html].
In [18]:
for n in range(len(county_shapes)):
x = [px[0] if px[0] <0 else px[0]-360 for px in county_shapes[n].points]
y = [px[1] for px in county_shapes[n].points]
plt.plot(x, y,'k.',ms=2)
plt.axis('scaled')
Out[18]:
But it is better to use polygons since most probably we will want to color in the counties:
In [19]:
fig, ax = plt.subplots(figsize=(12, 12))
for s in county_shapes:
color = np.random.uniform(0, 1, 3)
poly = mpl.patches.Polygon(s.points, facecolor=color, edgecolor='none')
ax.add_patch(poly)
ax.autoscale()
plt.show()
In [ ]: