Folium: How can I hide the legend for choropleth chart?

Created on 5 Sep 2018  路  8Comments  路  Source: python-visualization/folium

I have checked the parameters for the choropleth() function. However can't figure out how to disable the legend. Is there any way to do that?

Many thanks!

m.choropleth(
 geo_data, data=None, columns=None, key_on=None, threshold_scale=None, fill_color='blue', 
 fill_opacity=0.6, line_color='black', line_weight=1, line_opacity=1, name=None, legend_name='',
 topojson=None, reset=False, smooth_factor=None, highlight=None)
question

Most helpful comment

It's a bit of a hack, but you can delete the color scale after it was created:

m = folium.Map()
m.choropleth()
for key in m._children:
    if key.startswith('color_map'):
        del(m._children[key])

Why do you want to hide the legend? We should decide if this should be added as an argument.

All 8 comments

It's a bit of a hack, but you can delete the color scale after it was created:

m = folium.Map()
m.choropleth()
for key in m._children:
    if key.startswith('color_map'):
        del(m._children[key])

Why do you want to hide the legend? We should decide if this should be added as an argument.

BTW, you can probably use the GeoJson class to achieve that.

Closing b/c having a choropleth with the legend is not the goal of the .choropleth() method. What we can do is to add some example on how to use the GeoJson for that though.

There are a couple of reasons why I do not want the scale bars:

  • sometimes the scales are obvious
  • if I have multiple layers with the same scale, they get redundant.
  • If I have multiple layers they take way too much place
  • If I have multiple layers the scale bar blocks the zoom button making zooming impossible (+/-)

@Conengmo you might know how this works for newer folium versions? Using your approach seem not to work with newest version.

I didn't test it but does this work?

m = folium.Map()
choropleth = folium.Choropleth().add_to(m)
for key in choropleth._children:
    if key.startswith('color_map'):
        del(m._children[key])

@Conengmo no unfortunately not. it ends in a key error. I am trying to create a bunch of choropleth maps and by hide the layer in the layer control I would like to hide the legend as well.
Do you have any other suggestion? I would really appreciate it. I think the whole legend thing generally is an issue at Folium.

@renelikestacos In combination with this comment and this notebook I was able to delete the choropleth's legend and create a legend for each layer.

I found a slight variation of Conengmo comment works. Delete key in choropleth._children before adding to m:

m = folium.Map()
choropleth = folium.Choropleth()
for key in choropleth._children:
    if key.startswith('color_map'):
        del(choropleth._children[key])
choropleth.add_to(m)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xhx509 picture xhx509  路  15Comments

FlorianHoevelmann picture FlorianHoevelmann  路  14Comments

ibayer picture ibayer  路  21Comments

achourasia picture achourasia  路  19Comments

achourasia picture achourasia  路  15Comments