为 Folium 地图添加标题或文本

6

我想知道是否有办法在Python的folium地图上添加标题或文本?

我有8张地图要展示,我希望用户知道他们正在查看哪张地图,而不必点击标记。我试图添加地图图片,但由于我的声望分数不够高,所以无法添加。

我的代码:

#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]
2个回答

12
当然可以在Folium地图上添加标题。
例如:
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

有没有办法删除HTML子元素? - Ricardo Sanchez

1

标题叠加

如果您不想在地图容器之前添加一个HTML div,这是一个更清晰的HTML地图叠加效果。产生以下结果。

enter image description here

代码

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接