Search code examples
folium

Adding linear colorbar associated with markers on a folium map


I have the data points below

geo_pts = [(43.613104184487966, 3.888499606480643),
 (43.62380243834923, 3.857292924347734),
 (43.59878916204914, 3.8655616117551346),
 (43.619188835688256, 3.863774797275551),
 (43.609084246467894, 3.863354463170052),
 (43.59877648226874, 3.8949413546743608),
 (43.59641535416491, 3.9004649638210434),
 (43.60835143135754, 3.868352674165506),
 (43.61140610033741, 3.895321677934005),
 (43.59707832354887, 3.8988040501915093),
 (43.59098438823383, 3.8800897856962373),
 (43.60739277101043, 3.8667832414359857),
 (43.61663960717645, 3.895544460094556),
 (43.62348570738639, 3.865234073884166),
 (43.61294571774928, 3.8914346874368637)]

This list of color code:

col_hex=['#8000ff',
 '#5e35fe',
 '#3c68f9',
 '#1996f3',
 '#08bee9',
 '#2adddd',
 '#4df3ce',
 '#6efebe',
 '#90feab',
 '#b2f396',
 '#d4dd80',
 '#f6be68',
 '#ff964f',
 '#ff6835',
 '#ff351b']

that I am plotting on a map using:

fm = folium.Map(location=geo_pts[0], tiles="Stamen Terrain")
for num, loc in enumerate(geo_pts):
    folium.Marker(
        location=loc,
        popup="Pts" + '{:02d}'.format(num),
        icon=folium.Icon(color='white',icon_color=col_hex[num], icon="star", prefix="fa"),
    ).add_to(fm)

It gives me this map: folim_map_with_markers

How can I add a colorbar on this same map indicating the colorscale label (1:purple -> 15:red)?

Thank you


Solution

  • You can create a colorbar with this code :

    import branca.colormap as cmp
    step = cmp.StepColormap(
     col_hex,
     vmin=1, vmax=15,
     caption='Color Scale for markers'
    )
    

    And you add it to the map like this :

    step.add_to(fm)