folium

How to set the location of the Folium colorbar? I want to move the colormap from topright to topleft


I am trying to use colorbar for an output variable circle plot in Folium, and I want to move the colorbar location to topleft from topright. I could do this by editing the html file, but I prefer to embed this in the python script itself. Please suggest

colormap = cm.LinearColormap(colors=['green','red'], index=[min(df['output']), max(df['output'])], vmin=min(df['output']),vmax=max(df['output']), caption='output in units')

folium.Circle(location=[row['Latitude'], row['Longitude']], 
                        radius=800*row["output"],
                        fill=True,
                        # opacity=0.8,
                        # fill_opacity=1,
                        color=colormap(row["output"])).add_to(m)

colormap location cannot be set here.


Solution

  • A simple hack is to update the underlying script by setting the position to "topleft" :

    colormap.add_to(m)
    
    m.render() # to trigger the script
    m.get_root().script = folium.Element(
        m.get_root().script.render().replace("topright", "topleft")
    )
    

    Output (m) :

    enter image description here