Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choropleth legends being part of the layer #1052

Closed
ox-baloo opened this issue Jan 13, 2019 · 12 comments
Closed

Choropleth legends being part of the layer #1052

ox-baloo opened this issue Jan 13, 2019 · 12 comments
Labels
bug An issue describing unexpected or malicious behaviour

Comments

@ox-baloo
Copy link

Please add a code sample or a nbviewer link, copy-pastable if possible

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)

Problem description

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.

Expected Output

The legends for a folium.Choropleth object should disappear when the folium.Choropleth layer is deselected from the layer control box.

Output of folium.__version__

0.7.0

@Conengmo Conengmo added the bug An issue describing unexpected or malicious behaviour label Jan 13, 2019
@emc5ud
Copy link

emc5ud commented May 20, 2019

Even in maps with a single choropleth, being able to toggle the legend's visibility along with the choropleth helps reduce visual clutter.

@julianaddison
Copy link

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.

@kylenguyen245
Copy link

Much appreciate if this problem is solved as well! Thanks!!!

@emc5ud
Copy link

emc5ud commented Mar 4, 2020

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)

@kylenguyen245
Copy link

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?
image

@jjbenes
Copy link
Contributor

jjbenes commented Apr 1, 2020

@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)?
overlapping labels

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)

@AhadMomin
Copy link

AhadMomin commented Jan 29, 2021

does anyone have a fixed for this instead of hiding the legend? I have a choropleth with a lot of layers and actually need the colormap to be displayed for each of my layer being selected :(. would really appreciate help! . Currenlty, when I create a loop, all the legends for each layers get pasted on top of everything without the layers being selected.

@StevePuts
Copy link

I could also use a solution for this. I have maps with 40 layers.

@jomccall
Copy link

Same

@Malmurrani
Copy link

Hi,
any help
trying to remove the color scale or at least link one color scale to each layer. rather than showing all maps scales at the same time. thanks

@wwright21
Copy link

Looking for a solution here. I've got multiple choropleth layers and would like for each choropleth's legend to be part of the layer, such that selecting Choropleth 1 would show its corresponding legend, but then selecting Choropleth 2 would remove the first legend and show the legend corresponding to this second layer. It doesn't make much sense to continue showing the legend for Choropleth 1 layer even when it's turned off.

@Conengmo
Copy link
Member

Turns out this is actually a duplicate of #450

@Conengmo Conengmo closed this as not planned Won't fix, can't repro, duplicate, stale Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue describing unexpected or malicious behaviour
Projects
None yet
Development

No branches or pull requests