Libraries & Data

For creating this chart, we will need to load the following libraries:

import folium
from folium import plugins
import pandas as pd
import numpy as np

Dataset

Let's start by loading shape data:

url = "https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/earthquakes.csv"
df = pd.read_csv(url)
df = df[df['Depth (km)'] >= 0.01]  # depth of at least 10 meters
df.sort_values(by='Depth (km)', ascending=False, inplace=True)
df.head()
DateTime (utc)RegionMagnitudeDepth (km)LatitudeLongitudeModeMapyear
796120/02/201906:50:47Banda Sea5.02026-6.89129.15A-2019.0
681307/07/201907:50:53Eastern New Guinea Reg, P.N.G.5.41010-5.96147.90A-2019.0
829317/01/201914:01:50Fiji Islands4.7689-18.65179.44A-2019.0
1125803/01/201806:42:58Fiji Islands Region5.5677-19.93-178.89A-2018.0
953006/09/201818:22:24Fiji Islands Region5.8672-18.88179.30A-2018.0

Custom bubble style

The main steps to create a bubble map with folium are:

In order to make each point better looking, in the CircleMarker(), we:

# Initialize map
m = folium.Map(
    location=[0, 0],  # center around Africa
    zoom_start=2,  # dezoom
    tiles='cartodb positron'  # background style
)

# Add all the individual earthquakes to the map
for idx, row in df.iterrows():
    color = '#0a9396' if row['Magnitude'] < 4 else '#ee9b00' if row['Magnitude'] < 6 else '#ae2012'
    folium.CircleMarker(
        location=[row['Latitude'], row['Longitude']],
        radius=np.log(row['Depth (km)']) * 2,
        color=color,
        fill=True,
        fill_color=color,
        fill_opacity=0.5,
        weight=1
    ).add_to(m)

m.save('../../static/interactiveCharts/593-customize-bubble-map-with-folium-1.html')

Tooltip

An interactive map without a tooltip wouldn't be very effective!

Luckily, with the tooltip argument, customizing it is straightforward, and we can even incorporate some HTML into it.

For instance, we can display the Location, Magnitude, Depth, and Date, wrap them in <b> tags for boldness, and set them as level 5 headings (<h5>).

# Initialize map
m = folium.Map(
    location=[0, 0],
    zoom_start=2,
    tiles='cartodb positron'
)

# Add all the individual earthquakes to the map
for idx, row in df.iterrows():
    tooltip_text = f"""
   <h5><b>Location:</b> {row['Region']}</h5>
   <h5><b>Magnitude:</b> {row['Magnitude']}</h5>
   <h5><b>Depth:</b> {row['Depth (km)']} km</h5>
   <h5><b>Date:</b> {row['Date']}</h5>
   """
    color = '#0a9396' if row['Magnitude'] < 4 else '#ee9b00' if row['Magnitude'] < 6 else '#ae2012'
    folium.CircleMarker(
        location=[row['Latitude'], row['Longitude']],
        radius=np.log(row['Depth (km)']) * 2,
        color=color,
        fill=True,
        fill_color=color,
        fill_opacity=0.5,
        weight=1,
        tooltip=folium.Tooltip(tooltip_text, sticky=True)
    ).add_to(m)

m.save('../../static/interactiveCharts/593-customize-bubble-map-with-folium-2.html')

Automatically cluster points

folium comes with a set of plugins to add new features super easily!

For example here we use the MarkerCluster() plugin to automatically cluster the marker into groups when zooming-out.

And in order for this to work: we add the markers to the map with .add_to(marker_cluster) instead of .add_to(m)

from folium import plugins

# Initialize map
m = folium.Map(
    location=[0, 0],
    zoom_start=2,
    tiles='cartodb positron'
)

# Add earthquake data with clustering
marker_cluster = plugins.MarkerCluster().add_to(m)

# Add all the individual earthquakes to the map
for idx, row in df.iterrows():
    tooltip_text = f"""
   <h5><b>Location:</b> {row['Region']}</h5>
   <h5><b>Magnitude:</b> {row['Magnitude']}</h5>
   <h5><b>Depth:</b> {row['Depth (km)']} km</h5>
   <h5><b>Date:</b> {row['Date']}</h5>
   """
    color = '#0a9396' if row['Magnitude'] < 4 else '#ee9b00' if row['Magnitude'] < 6 else '#ae2012'
    folium.CircleMarker(
        location=[row['Latitude'], row['Longitude']],
        radius=np.log(row['Depth (km)']) * 2,
        color=color,
        fill=True,
        fill_color=color,
        fill_opacity=0.7,
        weight=0.4,
        tooltip=folium.Tooltip(tooltip_text, sticky=True)
    ).add_to(marker_cluster)

m.save('../../static/interactiveCharts/593-customize-bubble-map-with-folium-3.html')

Going further

You might be interested in: