Interactive Map visualization with Folium in Python
By: Saidakbar P
Tags: Folium, Data Visualization, Python, Mapping, Maps

When working with datasets, more often than not, we encounter property sales, rental or housing-related datasets. At first glance, we carry on doing the analysis with descriptive statistics to find out the patterns in the dataset. We, as humans, get better insights when we are presented with visualizations such as scatter plots, box plots, pie charts, bar charts and more. However, if we are dealing with datasets that have addresses, zipcodes or geographical locations, we do not have to limit our data analysis with line graphs. In facts, we have to explore the possibility of efficiently using the location points present in the dataset. In this article, I explore the folium library for interactive map visualizations. The code on jupyter notebook for this article is available on my Kaggle page.

NYC property sales in 2017 by Zipcodes. Medium platform does not allow Javascript in the articles. Check my Kaggle page for the interactive map version.

While doing an exploratory data analysis of New York City Property Sales, I tried to understand huge variations in property sale prices, which were distorting the bar charts with many outliers. Well, it turns out I should have aggregated property sales by zipcodes. Zipcode or postal code might be a better representation of how house prices vary in different regions in the city. Even then a line graph or bar chart may not be helpful in understanding how 167 different zip codes in NYC have property price variations. Folium library comes to help us. Folium is an easy-to-use interactive map visualization tool.

Getting Started

Before we start, I assume that the reader has some familiarity with Python and have Jupyter Notebook installed in their system. First, we have to install Folium library within Jupyter Notebook (or in the command line):

!pip install folium

Make sure you have an internet connection to install the library.

I did my analysis on a zip code level, but you can do map analysis within country or continent level as well. Folium has a very solid list of great examples on their page if you want to explore more. Today we will be concentrating on creating zipcode layer, adding markers, which shows some information about the location, to each of 167 zip codes and clustering markers together so the map does not become overwhelming to inspect.

Markers for each zip code

Let us take the above image as an illustration of what we want to accomplish:

Zipcode layers are boundaries of each zip code that covers some territory in the city. As we can see, some zipcode layers are purple meaning their average property sale prices are high whereas some zipcode layers are light blue or transparent white color meaning property sale prices are not as high. Dark regions mean no property sales occurred in that region in 2017.

are boundaries of each zip code that covers some territory in the city. As we can see, some zipcode layers are purple meaning their average property sale prices are high whereas some zipcode layers are light blue or transparent white color meaning property sale prices are not as high. Dark regions mean no property sales occurred in that region in 2017. Markers are blue in color and located in the center of each zip code. When we hover the mouse over markers, they show few details of the region and when we click on the markers, they show more details of the place.

are blue in color and located in the center of each zip code. When we hover the mouse over markers, they show few details of the region and when we click on the markers, they show more details of the place. Marker clusters are those green circle with a number on them. Marker clusters indicate how many markers are present in that location. This is a very convenient way of displaying markers in groups. Otherwise, the entire map of NYC will be filled with markers.

If we did not use a marker cluster, this is how our map would look: