Adding a Title or Text to a Folium Map
Asked Answered
T

2

8

I'm wondering if there's a way to add a title or text on a folium map in python?

I have 8 maps to show and I want the user to know which map they're looking at without having to click on a marker. I attempted to add an image of the map, but couldn't because I don't have high enough reputation score.

My code:

#marker cluster
corpus_chris_loc = [27.783889, -97.510556]

harvey_avg_losses_map = folium.Map(location = corpus_chris_loc, zoom_start = 5)

marker_cluster = MarkerCluster().add_to(harvey_avg_losses_map)

#inside the loop add each marker to the cluster
for row_index, row_values in harvey_losses.iterrows():

    loc = [row_values['lat'], row_values['lng']]
    pop = ("zip code: " + str(row_values["loss_location_zip"]) + "\nzip_avg: " + "$" + str(row_values['zip_avg'])) #show the zip and it's avg
    icon = folium.Icon(color='red')
    marker = folium.Marker(
        title = "Harvey: " + "$" + str(row_values['harvey_avg']),
        location = loc, 
        popup = pop,
        icon=icon) 

    marker.add_to(marker_cluster)


#save an interactive HTML map by calling .save()

harvey_avg_losses_map.save('../data/harveylossclustermap.html')

harvey_avg_losses_map[map of hurricane harvey insurance claims][1]
Through answered 21/5, 2020 at 5:43 Comment(0)
P
13

Of course you can add a title to a Folium map.

For example:

import folium

loc = 'Corpus Christi'
title_html = '''
             <h3 align="center" style="font-size:16px"><b>{}</b></h3>
             '''.format(loc)   

m = folium.Map(location=[27.783889, -97.510556],
               zoom_start=12)

m.get_root().html.add_child(folium.Element(title_html))

m.save('map-with-title.html')
m
Petrochemical answered 21/5, 2020 at 18:24 Comment(1)
Is there a way to remove the html child?Loudhailer
F
4

Title Overlay

A cleaner html map overlay if you do not want to add a html div before the map container. Produces this result.

enter image description here

Code

import folium
map_title = "Your map title"
title_html = f'<h1 style="position:absolute;z-index:100000;left:40vw" >{map_title}</h1>'
m.get_root().html.add_child(folium.Element(title_html))
m.save('map-with-title.html')
m
Forlini answered 8/11, 2023 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.