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.

Getting KeyError after using "bins" parameter in Choropleth

See original GitHub issue

I tried to do a Choropleth map using Folium. The result was ok but I wanted the colormap to be more “shaded”, since by default there is only 6 bins. However when I set the parameter “bins=10”, I get the error "KeyError: ‘YlGn’ " I noticed I have the same error, when I specify the “bins” parameters in the “Choropleth maps” example of the QuickStart. So I will use this example to illustrate my issue.

The following code:

import folium
import pandas as pd


url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data'
state_geo = f'{url}/us-states.json'
state_unemployment = f'{url}/US_Unemployment_Oct2012.csv'
state_data = pd.read_csv(state_unemployment)

m = folium.Map(location=[48, -102], zoom_start=3)

folium.Choropleth(
    geo_data=state_geo,
    name='choropleth',
    data=state_data,
    columns=['State', 'Unemployment'],
    key_on='feature.id',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='Unemployment Rate (%)',
    bins=10
).add_to(m)

folium.LayerControl().add_to(m)

m

Leads to this error :

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-6-d2bc56d78020> in <module>
     20     line_opacity=0.2,
     21     legend_name='Unemployment Rate (%)',
---> 22     bins=10
     23 ).add_to(m)
     24 

/usr/local/lib/python3.6/site-packages/folium/features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
   1088             # We add the colorscale
   1089             nb_bins = len(bin_edges) - 1
-> 1090             color_range = color_brewer(fill_color, n=nb_bins)
   1091             self.color_scale = StepColormap(
   1092                 color_range,

/usr/local/lib/python3.6/site-packages/branca/utilities.py in color_brewer(color_code, n)
    158     if not explicit_scheme:
    159         # Check to make sure that it is not a qualitative scheme.
--> 160         if scheme_info[base_code] == 'Qualitative':
    161             matching_quals = []
    162             for key in schemes:

KeyError: 'YlGn'

The same code without the " bins=10" works perfectly.

What Am I doing wrong here ? Is it because the color_brewer schemes only take 6 values ? What Am I supposed to do then ?

Python version : 3.6.8 Folium Version : ‘0.10.1’

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

1reaction
lorenzoromani1983commented, Aug 9, 2020

Hi. Is this fixed? I can only plot maps up to 9 bins. More than 9 bins still gives keyerror.

0reactions
Conengmocommented, Nov 23, 2022

I believe this was fixed with https://github.com/python-visualization/branca/pull/115, which was included in branca 0.6.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why KeyError occurs, when using bins parameter ... - Stack Overflow
No information is available for this page.
Read more >
Why Keyerror Occurs When Using Bins Parameter In Folium ...
I am working on Folium choropleth.When I enter 11 values on bins parameter KeyError occurs.The error doesn't occur when I enter under 10...
Read more >
[Code]-Choropleth Map - Folium Data Won't Show-pandas
Problem description: I'm experiencing an issue while creating a folium choropleth map in jupyter notebook. Everything is showing as expected except the data ......
Read more >
Folium draw polygons with distinct colours - GIS Stack Exchange
Choropleth object with bins parameter set to 116 (that's the ... But when I did this I get an error message: ... Sign...
Read more >
How to step up your Folium Choropleth Map skills
To get a better understanding of the parameters, we take a look at the documentation. According to the documentation, we learn the following:....
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