This short example shows how one can easily use
cartopy to beautifully plot polygons from
PySAL. Read your data with
PySAL, convert them to
matplotlib with its
viz module and set the right projection and other geo goodies with
This notebook (and the data required to run it), are hosted as a
gist on this link.
%matplotlib inline import matplotlib.pyplot as plt import pysal as ps import numpy as np from pysal.contrib.viz import mapping as maps import cartopy.io.img_tiles as cimgt import cartopy.crs as ccrs
London borough data from Robin Lovelace's tutorial on R (link). The shapefile
LondonBoroughs is reprojected to lon/lat and renamed
boroughs. For convenience, the shapefile is attached to the gist where this notebook is hosted.
shp = ps.open('boroughs.shp') bg = cimgt.OSM()
extent = [shp.bbox, shp.bbox, shp.bbox, shp.bbox] boros = maps.map_poly_shp(shp) boros.set_transform(ccrs.Geodetic()) boros.set_facecolor('none') f = plt.figure(figsize=(10, 8)) ax = plt.axes(projection=bg.crs) ax.set_extent(extent) ax.add_image(bg, 11) ax.add_collection(boros) f.suptitle('London boroughs, all together') plt.show()