Skip to content

Getting KeyError after using "bins" parameter in Choropleth #1254

@emontarsolo

Description

@emontarsolo

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn issue describing unexpected or malicious behaviour

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions