Choropleth Maps: Plotly & Folium

Kosta Tzioumis
Analytics Vidhya
Published in
3 min readSep 24, 2019

During a recent project based on New York City Restaurants I wanted to create some maps. It seemed to me like a useful thing to have on hand when talking about a city and they were very illustrative.The project ended up incorporating three choropleth maps based around New York Community Board Districts:

  • Density of restaurants
  • Most popular types of restaurants,
  • Average health department inspection scores,

All of these maps are made using the Folium library and I think they came out great. But there was one more map that I made but I didn’t end up using — Year on year changes in health dept inspection score averages. For a few reasons, including the overall sprawl over the project at this point, I did not end up including an analysis of time as a influencing factor on health department inspection scoring and grading. It was just as well because as much as I liked the idea behind that map, there were a few little nitpicking things I didn’t like about the the way it turned out.

You can view and interact with it here: https://ktzioumis.github.io/plotly-folium/m_ave_yronyr.html

The map displays the annual average health inspection score for each Community Board area for the years 2016 (from July), 2017,2018 & 2019 (to July). Each year can be selected or deselected as an overlay for the OpenStreetMap. The map as it is linked above is functional and illustrates the data well but my quibbles are as follows:

  • Selecting between choropleth maps does not deselect the other choropleths — makes it difficult to see changes from one map to the next, since they will either be overlayed as one is selected and the other deselected or the map goes blank in between selections
  • The colorbar for each choropleth is permanently displayed regardless of whether that choropleth is turned on

Folium does not support a method to resolve either of these problems.

I no longer needed to make this map but I still wanted to able to make this map. The shortcomings of Folium were disappointing so I wanted to try out another plotting/mapping library — Plotly.

Plotly Choropleth tutorial

The choropleth tutorial is based on unemployment data for US counties and I used it as a starting point to make a layered choropleth map the way I wanted it to look.

The process of creating a dropdown menu for choropleth selection I have adapted from this tutorial

github jupyter notebook

Animated gif of the result:

I love this result! And it addresses both my problems directly.

  • The dropdown allows for selection of choropleth that is tuned to set the visibility of the other choroploeth (A third option in the dropdown could be set to have both the choropleths visible should it be required).
  • The colorbar displays only the scale for the selected choropleth.

As a bonus because the colorbar is offset from the map to the right, the text for the xticks and label on the legend are not displayed in front of the map and are much more legible as a result. Folium does not allows for the legend position on the choropleth to be changed.

Definitely a win for Plotly

--

--

Kosta Tzioumis
Analytics Vidhya

Data, visualisations, machine learning. 2019 Flatiron Data Science Bootcamp Graduate but eternal data science student