Author avatar

Kimaru Thagana

Map Visualizations in Python Using Folium

Kimaru Thagana

  • Sep 17, 2020
  • 4 Min read
  • 27,695 Views
  • Sep 17, 2020
  • 4 Min read
  • 27,695 Views
Data Analytics
Python
Data
Machine Learning

Introduction

Data visualization projects sometimes have a geographical component. These may include the distribution of certain phenomena in a country, region, or continent, the geo-location of different items of interest, or other similar things. The geographical dimension is important and has to be part of the data visualization to drive the insight home.

In Python, tools exist that allow developers to generate maps with an extra layer of data representation and visualization. One of the resources that enables this is Folium, a library that combines the data analytics capabilities of Python and the mapping strengths of leaflet.js.

To get familiar with the library, consider a scenario where you would like to display store locations and net revenue in ($) of a popular fast food franchise in the African country of Kenya. For this task, the data is provided as a sample CSV below.

The guide assumes that you have at least intermediate level knowledge in Python and that you have a general understanding of geography and geo data.

Sample Data

For this plotting exercise, consider this CSV extract for sample data.

1store,latitude,longitude,revenue
2nairobifoods,-1.302473,36.7451004,600000
3kajiadofoods,-1.3999999,36.6293008,120000
4nakurufoods,-0.7944065,34.4749338,30000
5kisumufoods,-0.1950621,34.596355,40000
csv

Sample App

Since the visualization relies on leaflet.js, Folium is best experienced in a notebook environment like Google colab or Jupyter Notebooks. To set up, open a new notebook and run the installation for both Folium and pandas by running the command below on a new cell.

1pip install folium pandas
bash

On the next cell, import the required libraries and read the dataset.

1import folium
2import pandas as pd
3
4franchises = pd.read_csv('location/of/sample/csvfile')
5#view the dataset
6franchises.head()
python

The next step is to set up a map and view it. In a new cell, copy the code below.

1center = [-0.023559, 37.9061928]
2map_kenya = folium.Map(location=center, zoom_start=8)
3#display map
4map_kenya
python

The next and final step involves adding the location tags and popups of the franchise joints all over the country. In a new cell, copy the code below.

1for index, franchise in franchises.iterrows():
2    location = [franchise['latitude'], franchise['longitude']]
3    folium.Marker(location, popup = f'Name:{franchise["store"]}\n Revenue($):{franchise["revenue"]}').add_to(map_kenya)
4
5#display the map
6map_kenya
python

Once this is done, run all the cells in your notebook to load the CSV and view the maps.

Since the map is interactive, the user can click on each marker and view the franchise name and revenue generated.

Alternative Approach

The above example involves running Folium in a notebook environment. There is an alternate approach where the project can be developed locally in an IDE and the result generated as an HTML file. To run the same app with this approach, copy the code block below into the file folium_app.py.

1import folium
2import pandas as pd
3
4franchises = pd.read_csv('location/of/sample/csvfile')
5#view the dataset
6print(franchises.head())
7center = [-0.023559, 37.9061928]
8map_kenya = folium.Map(location=center, zoom_start=8)
9for index, franchise in franchises.iterrows():
10    location = [franchise['latitude'], franchise['longitude']]
11    folium.Marker(location, popup = f'Name:{franchise["store"]}\n Revenue($):{franchise["revenue"]}').add_to(map_kenya)
12
13# save map to html file
14map_kenya.save('index.html')
python

Run the above code and the index.html file will be generated. You can view it in the browser and interact with the map.

Sample Screen

Folium in action

Here's a look at Folium in action.

Conclusion

You now know how to visualize data with a geographical dimension using Folium maps. This skill is vital in any business role that involves data analytics, such as data scientist, business intelligence developer, data analyst, or GIS developer.

To further build and complement this knowledge of Python Folium, explore alternative Python mapping libraries, such as Bokeh Maps and Plotly Maps.