Monday, August 17, 2015

A Python script to make choropleth grid maps

In May 2015, there was a sudden fad in the Dataviz community (on Twitter, anyway) for hexagonal grid-type choropleth maps. A choropleth (not "chloropleth") is a map in which areas are filled in with a color whose intensity and/or hue is proportional to a quantity; we've all seen them. The problem with traditional choropleths is they are dependent on area so that, for example, a choropleth of the United States will emphasize relatively deserted Wyoming over highly populated Massachusetts.

One recent partial solution to this has been to create choropleths with equal representations of subunits with squares or hexagons. They're still not proportional to population (attempts to solve this last hurdle generally result in ugly and/or unrecognizable maps), but they're pretty cool nonetheless.

As soon as the hub-bub started, I thought it would be relatively painless and an interesting exercise to code up a Python script that would make these choropleths. Since they're geometric, it's a cinch to output SVG vector markup. I got about 90% of the way through this project in three weeks, and then a new job and some health issues interfered. but I finally found a weekend to finish it off, in at least a beta, v.0.1 way.

The script is hosted on my GitHub, and here's the IPython notebook/Jupyter tutorial that goes along with it. (There's also a script called Colorbin that's supposed to remove the hassle from associating colors with quantities for the choropleths).

Feedback is welcome. Here are some examples of what the script has produced:























• • •

3 comments:

  1. Nice results! I very much like the hexas.

    Where did you get the internet usage data for Europe?

    In the code I would advise to split functionality. Now you end up with one class containing all your logic. For example reading CSV data can be decoupled from the class. And list the imports at the start of the file, instead of in class methods.

    To get started you can find a lot of good point in the Python style guide: https://www.python.org/dev/peps/pep-0008/#imports

    ReplyDelete
    Replies
    1. > Where did you get the internet usage data for Europe?

      I'm afraid I don't recall! I lost track of where that came from.

      > For example reading CSV data can be decoupled from the class.

      Interesting... can you tell me why I would want to do this?

      > list the imports at the start of the file

      You are so right! I can't even remember why I did it that way, it was probably some sort of stopgap measure as I was developing the individual methods. How embarrassing!

      Delete