Map Visualization with Folium

What is Folium?

ferhatmetin
Data Science Earth
6 min readJan 13, 2021

--

Folium is a library that uses JavaScript leaflet.js module in the background and it enables interactive map visualizations in Python. Now let’s examine some features in Folium.

1. Map() and Tiles

Firstly, let’s take the necessary steps to use the Folium library.

We use the Map() function to create a map. For this, we need to give latitude and longitude information to the function. For example, let us enter the approximate latitude and longitude of Istanbul.

Let’s look at the output without any parameters.

Figure 1. Folium-Output-1

By default, “tiles” appears as ”OpenStreetMap” in the Map() function.This parameter allows us to change the style of the map. We can change this to Stamen Toner, CartoDB positron, Cartodb dark_matter, Stamen Watercolor or Stamen Terrain. we also adjust the size with the width and height parameters, and zoom_start lets you zoom in.

Figure 2. Folium-Output-2
Figure 3. Folium-Output-3

2. Circle(),CircleMarker() ve Marker()

We can use the Circle() function to circle the coordinates. Let’s look at the output by entering parameters such as radius and color.

Figure 4. Folium-Output-4

Another way to do this is to use the CircleMarker() function.

Figure 5. Folium-Output-5

Now let’s use the coordinates of Sakarya as an example and use the Marker() function to mark the location of the coordinates. Let’s set the location name by using the popup parameter.

Figure 6. Folium-Output-6

3. MiniMap()

Let’s call plugins from Folium firstly. In order to create a minimap, use the MiniMap() function.

Figure 7. Folium-Output-7

4. MarkerCluster()

For this example, I used the data “number of natural gas subscribers by counties” in the IBB Open Data Portal. This data consists of county names and subscriber count data.Since we need coordinates of Istanbul districts, you can pull the coordinate information from Google Maps or get it from platforms like GitHub. Now let’s start loading data in Excel format. For that, we need the Pandas library. After loading the data, let’s observe the first 5 values.

Figure 8. Folium-Output-8
Figure 9. Folium-Output-9

Since there are columns with the same name, we can combine this data by using the merge() function in Pandas.

Figure 10. Folium-Output-10

The MarkerCluster() function is included in Plugins. Let’s give the location information as a parameter.

Figure 11. Folium-Output-11
Figure 12. Folium-Output-12

5. HeatMap()

For heatmap, we can use heatmap() in Plugins. For that, we need to give the data in the appropriate format. Now let’s take the necessary steps and look at our output.

Figure 13. Folium-Output-13

6. GeoJson

GeoJson in the Folium helps us border the coordinates we want. We need a json file with coordinates to draw the boundaries. You can find it from the platforms like Github or you can draw these boundaries yourself and create a json file by using geojson.io. For this example, let’s use the borders of Turkey except Cyprus.

Figure 14. Folium-Output-14
Figure 15. Folium-Output-15
Figure 16. Folium-Output-16

7. Choropleth()

For this example, I am going to use the data “population number according to place of birth” on the Turkish Statistical Institute’s website. Now let’s load the data and do the necessary cleaning.

Figure 17. Folium-Output-17

Now let’s try to color the map by population.For this we can use the Choropleth () function. Here we can think of the parameter as key_on as a “primary key” i.e. an unique token for each property. It may be useful to open the text file to understand exactly what we should write.

Figure 18. Folium-Output-18

Implementation with Istanbul Airbnb Data

Let’s upload our data and examine it briefly, as we did before.

Figure 19. Folium-Output-19
Figure 20. Folium-Output-20

The price, latitude and longitude data show no missing value. Now let’s map their accommodation places by using each coordinate data. For this we can use the MarkerCluster() function.

Figure 21. Folium-Output-21

Now let’s do the same process using CircleMaker().

Figure 22. Folium-Output-22

Now our goal is to color the accommodation according to the prices. We need a colormap for that. We will give different colors for certain price ranges. To do this, let’s first assign the minimum and maximum prices to different variables.

Figure 23. Folium-Output-23

I created the colormap using cartil values. You can create it with different values and different colors.Then we can create our map using the Circle() function.

Figure 24. Folium-Output-24

In order to access the data sets I used in this article:

Population number data by place of birth: Türkiye İstatistik Kurumu Web Sitesi

İstanbul Airbnb data: Kaggle İstanbul Airbnb Verisi

Istanbul natural gas subscriber count data: İBB Açık Veri Portalı

All codes : https://github.com/ferhatmetin34/Map-Visualization-with-Folium

In order to access the resources I used:

https://nbviewer.jupyter.org/github/pythonvisualization/folium/tree/master

https://pypi.org/project/folium

--

--