question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Choropleth legends being part of the layer

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
jjbenescommented, 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)
0reactions
Conengmocommented, Nov 25, 2022

Turns out this is actually a duplicate of https://github.com/python-visualization/folium/issues/450

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automatically generating a legend for a choropleth layer in ...
This displays a choropleth based on the GeoJSON data in geojson , and uses a red-orange-yellow colourmap, basing the colours on the IMDRank ......
Read more >
Remove Existing Choropleth Map Layer, Legend and Info Div ...
The problem with my code is if I press the button twice all the things appear again on the map (i.e. GeoJson Layer...
Read more >
How to display an interactive choropleth map legend control
This tutorial shows how to display an interactive choropleth map legend control. ... the id of the first symbol layer in the map...
Read more >
Make a choropleth map, part 2: add interactivity | Help | Mapbox
The following code adds a legend to the map. To do so, it iterates through the list of layers you defined above and...
Read more >
Proportional and Choropleth Symbols Layer
a vector of colors. Note that if breaks is specified there must be one less colors specified than the number of break. colNA....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found