This page was generated from gallery/plotting_with_folium.ipynb.
Interactive online version: Binder badge

Plotting with Folium#

What is Folium?

Folium builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the leaflet.js library. This allows you to manipulate your data in Geopandas and visualize it on a Leaflet map via Folium.

In this example, we will first use Geopandas to load the geometries (volcano point data), and then create the Folium map with markers representing the different types of volcanoes.

Load geometries#

This example uses a freely available volcano dataset. We will be reading the csv file using pandas, and then convert the pandas DataFrame to a Geopandas GeoDataFrame.

# Import Libraries
import pandas as pd
import geopandas
import folium
import geodatasets
import matplotlib.pyplot as plt
/tmp/ipykernel_4269/ DeprecationWarning:
Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0),
(to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries)
but was not found to be installed on your system.
If this would cause problems for you,
please provide us feedback at

  import pandas as pd
df1 = pd.read_csv("volcano_data_2010.csv")

# Keep only relevant columns
df = df1.loc[:, ("Year", "Name", "Country", "Latitude", "Longitude", "Type")]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 63 entries, 0 to 62
Data columns (total 6 columns):
 #   Column     Non-Null Count  Dtype
---  ------     --------------  -----
 0   Year       63 non-null     int64
 1   Name       63 non-null     object
 2   Country    63 non-null     object
 3   Latitude   63 non-null     float64
 4   Longitude  63 non-null     float64
 5   Type       63 non-null     object
dtypes: float64(2), int64(1), object(3)
memory usage: 3.1+ KB
# Create point geometries
geometry = geopandas.points_from_xy(df.Longitude, df.Latitude)
geo_df = geopandas.GeoDataFrame(
    df[["Year", "Name", "Country", "Latitude", "Longitude", "Type"]], geometry=geometry

Year Name Country Latitude Longitude Type geometry
0 2010 Tungurahua Ecuador -1.467 -78.442 Stratovolcano POINT (-78.44200 -1.46700)
1 2010 Eyjafjallajokull Iceland 63.630 -19.620 Stratovolcano POINT (-19.62000 63.63000)
2 2010 Pacaya Guatemala 14.381 -90.601 Complex volcano POINT (-90.60100 14.38100)
3 2010 Sarigan United States 16.708 145.780 Stratovolcano POINT (145.78000 16.70800)
4 2010 Karangetang [Api Siau] Indonesia 2.780 125.480 Stratovolcano POINT (125.48000 2.78000)
world = geopandas.read_file(geodatasets.get_path(""))
ERROR 1: PROJ: proj_create_from_database: Open of /home/docs/checkouts/ failed
array(['Stratovolcano', 'Complex volcano', 'Shield volcano',
       'Subglacial volcano', 'Lava dome', 'Caldera'], dtype=object)
fig, ax = plt.subplots(figsize=(24, 18))
world.plot(ax=ax, alpha=0.4, color="grey")
geo_df.plot(column="Type", ax=ax, legend=True)
Text(0.5, 1.0, 'Volcanoes')

Create Folium map#

Folium has a number of built-in tilesets from OpenStreetMap, Mapbox, and CartoDB. For example:

# CartoDB Positron
map = folium.Map(location=[13.406, 80.110], tiles="CartoDB Positron", zoom_start=9)
Make this Notebook Trusted to load map: File -> Trust Notebook
# OpenStreetMap
map = folium.Map(location=[13.406, 80.110], tiles="OpenStreetMap", zoom_start=9)
Make this Notebook Trusted to load map: File -> Trust Notebook

Add markers#

To represent the different types of volcanoes, you can create Folium markers and add them to your map.

# Create a geometry list from the GeoDataFrame
geo_df_list = [[point.xy[1][0], point.xy[0][0]] for point in geo_df.geometry]

# Iterate through list and add a marker for each volcano, color-coded by its type.
i = 0
for coordinates in geo_df_list:
    # assign a color marker for the type of volcano, Strato being the most common
    if geo_df.Type[i] == "Stratovolcano":
        type_color = "green"
    elif geo_df.Type[i] == "Complex volcano":
        type_color = "blue"
    elif geo_df.Type[i] == "Shield volcano":
        type_color = "orange"
    elif geo_df.Type[i] == "Lava dome":
        type_color = "pink"
        type_color = "purple"

    # Place the markers with the popup labels and data
            popup="Year: "
            + str(geo_df.Year[i])
            + "<br>"
            + "Name: "
            + str(geo_df.Name[i])
            + "<br>"
            + "Country: "
            + str(geo_df.Country[i])
            + "<br>"
            + "Type: "
            + str(geo_df.Type[i])
            + "<br>"
            + "Coordinates: "
            + str(geo_df_list[i]),
            icon=folium.Icon(color="%s" % type_color),
    i = i + 1
Make this Notebook Trusted to load map: File -> Trust Notebook

Folium Heatmaps#

Folium is well known for its heatmaps, which create a heatmap layer. To plot a heatmap in Folium, you need a list of latitudes and longitudes.

# This example uses heatmaps to visualize the density of volcanoes
# which is more in some parts of the world compared to others.

from folium import plugins

map = folium.Map(location=[15, 30], tiles="Cartodb dark_matter", zoom_start=2)

heat_data = [[point.xy[1][0], point.xy[0][0]] for point in geo_df.geometry]


Make this Notebook Trusted to load map: File -> Trust Notebook