
Data visualization is one of the most overlooked areas of Data Science. Machine learning and statistical analysis are important, but being able to visualize data, especially different types of data, is a key aspect of data storytelling. While many introductory data science bootcamps and courses will cover how to create basic plots with matplotlib and seaborn, several of them do not cover how to visualize geographic data on maps.
Folium is a Python library that uses Leaflet.js and Open Street Map data to create high-quality map visualizations. In this article, I will demonstrate how you can use Folium to generate interactive map visualizations.
Installation
We can easily install Folium using pip as demonstrated below.
pip install folium
Import Libraries
As usual, I will import some Folium and some other libraries that we may need before getting started. Keep in mind you can find all of the code for this tutorial on GitHub.
import numpy as np
import pandas as pd
import folium
Plotting a Default World Map
Folium comes with a Map function that we can call with no arguments to generate a default world map.
folium.Map()

Notice how the default map is a projection and contains zoom controls on the top left corner so that we can interact with the map and focus on a specific region if needed.
Plotting Locations by Coordinates
We can create a map centered around a specific location by adding a list of coordinates to the location parameter in the Map function. Using the code below, we can generate a Map centered around Tampa, Florida.
folium.Map(location=[27.950575, -82.457176])

Changing the Map Tiles
We can also change the map tiles to create maps that look different with the "tiles" argument. We can use the Stamen Toner tiles described in detail here.
folium.Map(location=[27.950575, -82.457176], tiles="Stamen Toner")

Notice how the same map of Tampa is now black and white. We can also create terrain maps as demonstrated below.
folium.Map(location=[27.950575, -82.457176], tiles="Stamen Terrain")

We can also create a beautiful watercolor map with Stamen tiles as shown in the code below.
folium.Map(location=[27.950575, -82.457176], tiles="Stamen Watercolor")

Adding Map Markers
If we want to add markers to a map we can use the Marker class from Folium to create marker objects and add them to the map as demonstrated below. As expected, we can specify the location of a marker using coordinates.
m = folium.Map(location=[27.950575, -82.457176], tiles="Stamen Terrain")
folium.Marker(
location=[27.9658533, -82.8001026],
popup="Clearwater",
icon=folium.Icon(icon="cloud")
).add_to(m)
folium.Marker(
location=[27.773082733154297, -82.64020538330078],
popup="St. Petersburg",
icon=folium.Icon(color='green')
).add_to(m)
m

Notice how I have created markers on the map above on Clearwater and Saint Petersburg.
Creating a Choropleth Map
While creating maps is a useful feature, being able to plot and visualize data and trends on maps is even better. Folium gives us the ability to create choropleth maps as well. In this section, I will demonstrate how to create a choropleth map with U.S. unemployment data.
Reading the Data
The dataset we will be using is available in the examples directory on the Folium GitHub repo. We will read both a CSV file with unemployment statistics for each state in October 2012 as well as a GeoJSON file for generating the map.
url = (
"https://raw.githubusercontent.com/python-visualization/folium/main/examples/data"
)
state_geo = f"{url}/us-states.json"
state_unemployment = f"{url}/US_Unemployment_Oct2012.csv"
state_data = pd.read_csv(state_unemployment)
state_data.head(10)

The image above shows the first ten rows of the state unemployment data displayed using the pandas head function.
Generating the Map
Now that we have the unemployment data, we can generate a choropleth map. We will supply the following arguments to the Choropleth constructor:
- geo_data: a path to a file with GeoJSON data for generating the map.
- name: the name of the map
- data: the actual data that will be used to generate colors in choropleth map.
- columns: the columns of the data that will be used.
- key_on: the variable in the GeoJSON file to bind the data to (must start with feature, ie. feature.id)
- fill_color: the color scheme to use for the choropleth map
- fill_opacity: the opacity of the color fill.
- line_opacity: the opacity of the lines in map.
- legend_name: the name of the legend in the map.
m = folium.Map(location=[46, -102], zoom_start=3)
folium.Choropleth(
geo_data=state_geo,
name="choropleth map",
data=state_data,
columns=["State", "Unemployment"],
key_on="feature.id",
fill_color="Reds",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="Unemployment Rate (%)",
).add_to(m)
folium.LayerControl().add_to(m)
m

As you can see above, we were able to generate a choropleth map that visualizes the unemployment rate in each U.S. state.
Saving the Map as an HTML File
We can also save maps as HTML files, which is helpful for embedding Folium maps in web applications.
m.save('choropleth_unemployment.html')
Summary
Folium is a useful Python library for visualizing geographic data. It allows us to create interactive map visualizations that can also be saved as HTML files and embedded in web applications. I have only covered some of the basic features of Folium in this article and you can find a more detailed list of features on the Folium documentation page. You can also find all the code used in this article on GitHub.
Join my Mailing List
Join my mailing list to get updates on my data science content. You’ll also get my free Step-By-Step Guide to Solving Machine Learning Problems when you sign up! You can also follow me on Twitter for content updates.
And while you’re at it, consider joining the Medium community to read articles from thousands of other writers as well.
Sources
- Rob Story, Folium 0.14.0 documention, (2013), GitHub.