Map this… Comparing choropleth maps in Folium and Tableau Public

A Gordon
DataExplorations
Published in
4 min readOct 16, 2018

After my recent struggles to generate the GeoJSON file for Folium, I wanted to compare some tools for creating Choropleth maps to see which is the easiest to use going forward. For this test, I’ll use the 2016 Canada Census Population data organized by FSA (Forward Sortation Area — the first three characters of a Canadian Postal Code) to illustrate the relative population density of Toronto FSAs.

Folium

Folium is quite straight forward provided you already have a GeoJSON file for your area. If you do not, then generating one can get a bit nasty (see my earlier post about how to generate a GeoJSON file for Toronto FSAs).

First we’ll import our libraries and read in the census data. We’ll filter out the extraneous information and focus only on 2016 Population data for Ontario

import folium # map rendering library
import json
import numpy as np
import pandas as pd
df_pop = pd.read_csv("./data/T1201EN.CSV")
# we can immediately drop the columns we're not interested in
df_pop=df_pop[['Geographic code','Province or territory', 'Population, 2016']]
df_pop=df_pop[(df_pop['Province or territory']== "Ontario")]
df_pop.head()

To generate the map in Folium, you first tell it the coordinates where it should center the map and how zoomed out it should be. You can also indicate what type of base map to use (see the docs here.) The code snippet below centers on Toronto at a reasonable default zoom level for a city

  • small numbers = zoomed out (i.e. a zoom level of 5 is pretty good for a map of Canada)
  • large numbers = zoomed in
map_toronto = folium.Map(location=[43.653963, -79.387207], zoom_start=10)

Next, tell Folium where to find your GeoJSON file

ontario_geo = "./data/Toronto2.geojson"

And, finally, define the map

  • geo_data: the path to the GeoJSON file
  • data: the dataframe with the data to be shown
  • columns: a list of the data frame columns you want to use
  • → Geographic code: this contains the FSA and will be mapped to the GeoJSON file according to key_on parameter
  • → Population, 2016: the variable to use to colour in the different FSAs
  • key_on: how to match up the dataframe and the GeoJSON file. This is where the FSA is stored in the json file
{ "type": "Feature", "properties": { "CFSAUID": "M9R", "PRUID": "35", "PRNAME": "Ontario" }, "geometry":
  • fill_color: you can choose from one of these palettes: ‘BuGn’, ‘BuPu’, ‘GnBu’, ‘OrRd’, ‘PuBu’, ‘PuBuGn’, ‘PuRd’, ‘RdPu’, ‘YlGn’, ‘YlGnBu’, ‘YlOrBr’, and ‘YlOrRd’.
map_toronto.choropleth(geo_data=ontario_geo,
data = df_pop,
columns=['Geographic code','Population, 2016'],
key_on='feature.properties.CFSAUID',
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Population by FSA')

map_toronto

Pretty straight forward, provided you have (or can find) the GeoJSON file

Tableau Public

If you don’t have access to GeoJSON, Tableau Public may save a lot of time and aggravation. It turns out in interpret and plot the FSAs without a need for any supporting files.

I opened the same census data set, hid the extra columns and filtered for the Toronto FSAs

Next I clicked on one of the worksheet tabs

  • Tableau has added Geographic code as a Dimension for us
  • Hover over Geographic Code and click to expand the drop down
  • Select Geographic role and choose “Zip Code/Postcode
  • you should now see a little globe icon next to Geographic code
  • double-click on Geographic code and Tableau will automatically generate a map and add points to the map for each FSA
  • Pretty easy! To turn this into a choropleth map, drag Population 2016 over the color Mark
  • and voila!

Final Thoughts

In most cases, it’s preferable to generate a map within your python project and Folium is easy to use to accomplish that. However, if you don’t have the necessary GeoJSON files and need a chart quickly, Tableau Public is an excellent option to explore.

(nb: I also wanted to compare choropleth charts in Altair, but it seems to require the installation of GeoPandas which is failing in my anaconda environment due to dependency conflicts with the Altair package. I didn’t have time to resolve those issues, but again it does highlight how useful it can be to fallback on a tool like Tableau Public when you need a chart quickly)

--

--