Getting KeyError after using "bins" parameter in Choropleth
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:2
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi. Is this fixed? I can only plot maps up to 9 bins. More than 9 bins still gives keyerror.
I believe this was fixed with https://github.com/python-visualization/branca/pull/115, which was included in branca 0.6.0.