Choropleth Maps in Python

Anhduy Nguyen
3 min readMay 30, 2022

--

Similar to my most recent post about fuzzy/percentage matching the inspiration for this post comes from my most recent project on housing data in the Seattle, Washington area. My collaborator and I though it would be cool to show a heat/choropleth map of the zip codes based on pricing and it ended up looking very nice. The struggle was finding all the documentation and the little blogs/guides/walkthroughs of making the map/graph the way we wanted it. I hope for those that are reading this post find it helpful.

This was the final product of our choropleth map.

Install Plotly

First and foremost you will need to install Plotly for Python. I won’t be going over that section. Plotly does that very well themselves and I’ll link to there installation page here https://plotly.com/python/getting-started/.

Importing the appropriate files

I will first be importing the appropriate libraries of Pandas for work with dataframes, json to read the geojson files that go into the map making, and plotly.express the tool used to make the choropleth maps. My data set had contained zip codes so I specifically looked for geojson data with zip codes. Here is a link to the repository of geojson that I used, but if your data has coordinates such as longitude and latitude that works also. I than read the geojson data into Washinton variable and my dataframe into df.

Choropleth Map Making

I than average out my dataframe data on zip codes for easier mapping. The code segment below shows you how to specifically create the choropleth map.

I will go over line by line what every part means.

fig : is used to create the chart/map

px.choropleth : is the module express from the plotly library, and choropleth is a method of express

data_frame : is the data frame we are using to plot data onto the map

locations : is the name of the column from the dataframe we are specifically tying to the geojson data for me this would be zipcode

featureidkey : is the location or the index we are tying the locations parameter to. In my case this would be properties.ZCTA5CE10 which refers to zip codes in my geojson data

geojson : is the geojson data we retrieved earlier to make the background of the choropleth map

color : is the measure from the dataframe we are using to compare to the location for me this is price

color_continuous_scale : is the color schema being used

labels : Shows the labels of the graph

fig.update_geos : is used to zoom in on the locations of the dataframe

Final Product

This is what the final product of the plotly map looks like. Because this is a png you can’t hover over the zip codes to show the pricing amount.

This was a fun tool I used for my project and I hope this blog helps out others too. The difference in the looks of the final product using plotly and the original choropleth map was down to the difference in geojson data used.

--

--