Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Plotting choropleth maps with Cartopy @ PyData ...

alinagator
November 03, 2015

Plotting choropleth maps with Cartopy @ PyData London

alinagator

November 03, 2015
Tweet

Other Decks in Programming

Transcript

  1. There are online tools that do this • CartoDB -

    cartodb.com • Google Fusion Tables - bit.ly/g-fusion • OpenHeatMap - openheatmap.com What about Python??
  2. Let’s plot a map of the world import matplotlib.pyplot as

    plt import cartopy.crs as ccrs ax = plt.axes(projection=ccrs.PlateCarree()) ax.coastlines() ax.stock_img()
  3. I’m only interested in the UK, so let’s zoom in

    import matplotlib.pyplot as plt import cartopy.crs as ccrs ax = plt.axes(projection=ccrs.PlateCarree()) ax.coastlines() ax.set_extent([-12, 3, 49, 60]) // x0,x1,y0,y1
  4. Increase the resolution of the coastline import matplotlib.pyplot as plt

    import cartopy.crs as ccrs ax = plt.axes(projection=ccrs.PlateCarree()) ax.coastlines(resolution='50m') ax.set_extent([-12, 3, 49, 60])
  5. Change the projection to Mercator import matplotlib.pyplot as plt import

    cartopy.crs as ccrs ax = plt.axes(projection=ccrs.GOOGLE_MERCATOR) ax.coastlines(resolution='50m') ax.set_extent([-12, 3, 49, 60])
  6. To add boundaries, we need shapefiles • A format for

    storing the location, shape, and attributes of geographic features • Found online (we used ONS - bit.ly/ons-boundaries) • Stored as a set of related files (don’t just download the .shp file)
  7. Let’s add regional boundaries import matplotlib.pyplot as plt import cartopy.crs

    as ccrs from cartopy.io.shapereader import Reader from cartopy.feature import ShapelyFeature file = '../uk_regions/uk_regions.shp' ax = plt.axes(projection=ccrs.GOOGLE_MERCATOR) ax.coastlines(resolution='50m') ax.set_extent([-12, 3, 49, 60])
  8. Let’s add regional boundaries import matplotlib.pyplot as plt import cartopy.crs

    as ccrs from cartopy.io.shapereader import Reader from cartopy.feature import ShapelyFeature file = '../uk_regions/uk_regions.shp' ax = plt.axes(projection=ccrs.GOOGLE_MERCATOR) ax.coastlines(resolution='50m') ax.set_extent([-12, 3, 49, 60]) regions = ShapelyFeature(Reader(file).geometries(), ccrs.PlateCarree(), facecolor=‘grey') ax.add_feature(regions)
  9. Plot the data ax = plt.axes(projection=ccrs.GOOGLE_MERCATOR) ax.coastlines(resolution='50m') ax.set_extent([-12, 3, 49,

    60]) norm = matplotlib.colors.Normalize(vmin=-6, vmax=4) cmap = plt.cm.gray_r for i, row in df.iterrows(): region = ShapelyFeature(df['shape'][i], ccrs.PlateCarree(), facecolor= cmap(norm(df[‘dev’][i])), ) ax.add_feature(region)
  10. Plot the data ax = plt.axes(projection=ccrs.GOOGLE_MERCATOR) ax.coastlines(resolution='50m') ax.set_extent([-12, 3, 49,

    60]) norm = matplotlib.colors.Normalize(vmin=-6, vmax=4) cmap = plt.cm.gray_r for i, row in df.iterrows(): region = ShapelyFeature(df['shape'][i], ccrs.PlateCarree(), facecolor= cmap(norm(df[‘dev’][i])), ) ax.add_feature(region)
  11. Plot the data ax = plt.axes(projection=ccrs.GOOGLE_MERCATOR) ax.coastlines(resolution='50m') ax.set_extent([-12, 3, 49,

    60]) norm = matplotlib.colors.Normalize(vmin=-6, vmax=4) cmap = plt.cm.gray_r for i, row in df.iterrows(): region = ShapelyFeature(df['shape'][i], ccrs.PlateCarree(), facecolor= cmap(norm(df[‘dev’][i])), ) ax.add_feature(region)