Interactive Maps with Folium

Learn about creating interactive maps through the Folium package.

Introduction

Understanding and interpreting geographic data is a vital aspect of modern data science, and this task often involves producing interactive maps. The ability to present and explore data within a spatial context significantly enhances comprehension and facilitates effective decision-making. This is why interactive maps are not just visually compelling, but are a critical tool for converting geospatial data into actionable insights.

The production of interactive maps, however, requires specialized tools. This is where the Folium package, comes into play. Folium is a powerful and flexible tool that helps data scientists and GIS professionals create dynamic and interactive maps, combining the data manipulation strengths of Python with the mapping strengths of the Leaflet library.

Note: Leaflet is an open-source JavaScript library to create interactive, mobile-friendly maps, and it is extensively used in numerous web-GIS applications due to its simplicity and performance.

Folium allows users to manipulate data in Python, and then visualize it on an interactive Leaflet map via a seamless integration. It accomplishes this by bridging the gap between Python and Leaflet, translating Python code into HTML, CSS, and JavaScript to create rich, interactive visualizations. This is particularly helpful, as Leaflet is one of the most well-regarded libraries when it comes to creating interactive maps, but it operates primarily in JavaScript.

This lesson is not intended to be a full Folium course, but a quick introduction to its basic usage and capabilities and how we can integrate it with our GeoPandas datasets.

Creating a basic Folium map

Creating an interactive map with Folium is pretty straightforward. Basically, what we need is to create a Folium Map instance. If no arguments are passed, it creates a map covering the world's extents. In this first example, let's pass the coordinates of the Golden Gate, in San Francisco, through the location argument (line 6), and specify the zoom factor (i.e., zoom start argument) to get a closer look of the bridge (line 7).

Note: The default zoom_start value is 10, and it ranges from 0 (farthest) to 18 (closest).

We can also control the size of the map with the width and height parameters (lines 8 and 9). Once the Map object is created, we can visualize it just by typing the name of the map object in a Jupyter notebook cell. If we're not using a Jupyter notebook, as is the case here, we can save the map as an .html file and display it in a web browser or other engine that's able to render HTML.

The code widget below is able to treat the output as HTML code, so we’ll create a helper function called show_map that saves the Folium map to a file-like object and then prints it to the console (lines 12–16). Another more straightforward approach would be to access the private method _repr_html() that outputs the HTML as a string, as commented in line 20.

Get hands-on with 1400+ tech skills courses.