📩 Load the geoJson file
The coordinates are stored at geoJson format, probably the most common file format for this kind of information. If you want to know more about this, visit the background map section of the gallery that gives a tour on how to build a map with python.
Now, let's load this geoJson file from the web with geopandas
That's it, we now have a geo dataframe containing all the counties as hexagones. Before starting to draw a map with it, you can have a look at it with:
| 1219 | 2015-05-13T22:26:15+00:00 | 2015-10-05T16:00:16+00:00 | Maine | 60.5 | ME | Maine (United States) | POLYGON ((-72.62574 55.31320, -69.90286 54.408... |
| 1217 | 2015-05-13T22:24:35+00:00 | 2015-10-05T16:00:16+00:00 | R.I. | 47.8 | RI | Rhode Island (United States) | POLYGON ((-72.62574 49.57439, -69.90286 48.544... |
🙇 Basic hexbin map
GeoPlot is probably the best python library to build a map from a geo dataframe. It is pretty straightforward thanks to its polyplot() function.
🖊️ Add county names
Let's add county names at the centroid of each. Fortunately, centroids are already available from the geometry column of the geo dataframe. For instance, you can access it for the first county with df.head(1).geometry.centroid.
Let's add a centroid column to the original dataframe:
Now let's plot both the county borders and their labels at centroids position.
🛠️ Load & merge numeric data
Let's transform the useless hexbin map above in a choropleth map, where each county is colored according to its mariage rate (# of mariage per 1000 inhabitants). The dataset is available here
It is now possible to create a first choropleth map as follow:
✨ Final map
Let's add the final touch to make it look good. Title, annotation, better color scale and more:
Note: there are still some stuff that could be improved here but it's getting late 😴