This is how I created and added the folium.Choropleth layers to the folium.Map object (self.map is a default folium.Map object)
folium.Choropleth(geo_data=self.geo_json,
data=self.map_data,
columns=[self.CODE_COL,self.SCORE_COL],
key_on='feature.properties.' + self.code_name,
fill_color=self.color,
fill_opacity=0.6,
line_opacity=0.2,
bins=self.scale,
legend_name=self.legend_label,
name=name,
nan_fill_color='purple',
show=show).add_to(self.map)
This is how I added folium.LayerControl and saved the file
folium.LayerControl(collapsed=False).add_to(self.map)
self.map.save(self.out_file)
I created a folium.Map with two folium.Choropleth objects added to it as overlays, so that someone viewing the .html file could choose which choropleth map they would like to see using the LayerControl. When the layers were deselected the choropleth layers were removed from the screen but the legend stayed.
I found this behaviour confusing as, when a choropleth layer was deselected the legend remained on the screen, I would expect the legends to belong to the layers on the map and appear and disappear as layers were selected/deselected.
The legends for a folium.Choropleth object should disappear when the folium.Choropleth layer is deselected from the layer control box.
folium.__version__0.7.0
Even in maps with a single choropleth, being able to toggle the legend's visibility along with the choropleth helps reduce visual clutter.
Hi, any chance if this has been fixed or if there's a workaround that anyone's aware of, I would be most grateful. Thanks.
Much appreciate if this problem is solved as well! Thanks!!!
You can do a hacky workaround by saving your map to html and adding the following lines:
map.on('overlayadd', function (eventLayer) {
$('.legend').show();
});
map.on('overlayremove', function (eventLayer) {
$('.legend').hide();
});
This will only work in specific cases though (one choropleth as your only overlay and only 1 legend object)
Hey emc5ud, thanks for your reply. I wonder if there is anyway to just remove the legend completely within Python script? I see a thread mentioned about this and tried to implemented it but it doesn't work. Do you have any thoughts on this?

@julianaddison The Python method @kylenguyen245 mentioned does work in principle. Delete color_map children outside of the loop. Hope this helps.
@kylenguyen245 I sometimes delete the legend (color maps) because the numerical labels overlap one another. I often don't use linear spacing for the bins. Do you know whether there's a way to use fewer labels than there are bins to avoid overlapping labels (see picture)?

def folium_del_legend(choropleth: folium.Choropleth):
"""A hack to remove choropleth legends.
The choropleth color-scaled legend sometimes looks too crowded. Until there is an
option to disable the legend, use this routine to remove any color map children
from the choropleth.
Args:
choropleth: Choropleth objected created by `folium.Choropleth()`
Returns:
The same object `choropleth` with any child whose name starts with
'color_map' removed.
"""
del_list = []
for child in choropleth._children:
if child.startswith('color_map'):
del_list.append(child)
for del_item in del_list:
choropleth._children.pop(del_item)
return choropleth
m = folium.Map(...)
folium_del_legend(
folium.Choropleth(...)).add_to(m)
Most helpful comment
@julianaddison The Python method @kylenguyen245 mentioned does work in principle. Delete color_map children outside of the loop. Hope this helps.
@kylenguyen245 I sometimes delete the legend (color maps) because the numerical labels overlap one another. I often don't use linear spacing for the bins. Do you know whether there's a way to use fewer labels than there are bins to avoid overlapping labels (see picture)?
