So You’d Like To Make a Map Using Python

So You’d Like To Make a Map Using Python by Stephan Hügel.

From the post:

Making thematic maps has traditionally been the preserve of a ‘proper’ GIS, such as ArcGIS or QGIS. While these tools make it easy to work with shapefiles, and expose a range of common everyday GIS operations, they aren’t particularly well-suited to exploratory data analysis. In short, if you need to obtain, reshape, and otherwise wrangle data before you use it to make a map, it’s easier to use a data analysis tool (such as Pandas), and couple it to a plotting library. This tutorial will be demonstrating the use of:

  • Pandas
  • Matplotlib
  • The matplotlib Basemap toolkit, for plotting 2D data on maps
  • Fiona, a Python interface to OGR
  • Shapely, for analyzing and manipulating planar geometric objects
  • Descartes, which turns said geometric objects into matplotlib “patches”
  • PySAL, a spatial analysis library

The approach I’m using here uses an interactive REPL (IPython Notebook) for data exploration and analysis, and the Descartes package to render individual polygons (in this case, wards in London) as matplotlib patches, before adding them to a matplotlib axes instance. I should stress that many of the plotting operations could be more quickly accomplished, but my aim here is to demonstrate how to precisely control certain operations, in order to achieve e.g. the precise line width, colour, alpha value or label position you want.

I didn’t catch this when it was originally published (2013) so you will probably have to update some of the specific package versions.

Still, this looks like an incredibly useful exercise.

Not just for learning Python and map creation but deeper knowledge about particular cities as well. On a good day I can find my way around the older parts of Rome from the Trevi Fountain but my knowledge fades pretty rapidly.

Creating a map using Python could help flesh out your knowledge of cities that are otherwise just names on the news. Isn’t that one of those quadruple learning environments? Geography + Cartography + Programming + Demographics? That’s how I would pitch it in any event.

I first saw this in a tweet by YHat, Inc.

Comments are closed.