Choropleth maps are a result of mixing spatial data with statistical data. A quick example would be a map showing the number of COVID-19 cases worldwide. We widely use choropleth maps to transmit information belonging to socio-economic data, climatic variables, etc. Typically, the choropleth maps are in 2D with a 1D colorbar (could be discrete or continuous). There exist some lesser-known choropleth maps like 3D choropleth maps and bivariate choropleth maps (with a 2D colorbar). In this article, I will focus on the most common type i.e., 2D maps with 1D colorbar.
The Necessities
Firstly, let’s go through some necessary things before jumping into the plotting. As you might have guessed, there are many ways to make a choropleth map. Here are some of the things we need –
- Choice of Python library – We have libraries such as Plotly, Geopandas, Folium, etc, for geospatial visualization. Plotly is beginner friendly, but I recommend geopandas for having better control on the output visualization.
- Statistical data – The variable which we are interested in plotting e.g. temperature.
- Shapefiles – These files hold the geometry data of different regions like cities, states, countries, etc.
- Choice of map projection.
I will show some examples of choropleth Maps at different resolutions – global, state/regional, and city levels.
Global Level
For the above choropleth map, I used Plotly Express, which is a simple method to generate a map. The statistical variable here is the world population and for the shapefile, I used the world administrative boundaries dataset. Below is the code to replicate the map –
I like to merge the dataset and the shapefile before plotting as it is convenient. As you can see, the code is quite short and easy to understand. If we want to change the resolution of the map to a regional level, the main change we make to the code is at the scope
parameter.
Let’s check the regional-level maps using the Geopandas library.
Regional Level
The above choropleth map is made using Geopandas library. It shows different agricultural exports in the US at the state level. You can download US shapefiles here. The code is as follows:
A bit heavy on code with this one, though it is easy to understand as it uses Matplotlib engine for plotting. I merge the dataset and the shapefile before plotting as earlier. I included different techniques to control the colorbar’s size, label and location. The same is possible with Plotly; since I am more used to working with Matplotlib, I prefer this.
County Level
Let’s check another regional-level choropleth map at the US county level. We follow the same procedure as above but include the US county shapefiles. Below is the choropleth map showing the unemployment rate in the US for different counties.
Here’s the code:
The code is slightly different as I included both county and state shapefiles here. I used grey color for the county boundary lines and black for the state lines to show clear demarcation between the different states and counties.
City Level
In this example, the city of Paris is shown with its different arrondissements. The colors represent the perimeter of each arrondissement. I added a background map to add more details and context to the visualization. The code for this plot is as follows:
The code is similar to the previous ones with a minor addition being the background map which is made using the contextily
library. You can pick different terrain styles for the map (check here).
Conclusion
There are different ways to generate choropleth maps. I have shown a few easy methods in this article. It is simple to plot using geopandas as well as Plotly. I might be biased over geopandas as I spent more time working with it but I use both libraries depending on the case.
Note:
All the datasets used in this article have free-to-use licenses. Links below:
- World bank data (Creative Commons Attribution 4.0 license)
- Shapefile data (Open Government Licence v3.0)
- Plotly datasets (MIT license)
Thanks for reading and cheers!
Want to Connect?
Reach me at LinkedIn, Twitter, GitHub or Website!