`IndexError` in Choropleth when providing `bins` as a sequence.
See original GitHub issueFirstly, thanks to the contributors for this great package, it makes it really easy to make pretty awesome looking maps.
Please add a code sample or a nbviewer link, copy-pastable if possible
Sample code to generate error:
column_name = 'example_column_name'
bin_intervals = [0,10,20,30,40,50,100]
folium.Choropleth(
geo_data=gdf,
name=f'{column_name} Choropleth',
data=data,
columns=['id', column_name],
key_on='feature.properties.id',
bins=bin_intervals,
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name=f'{column_name} (%)',
show=True
).add_to(m)
Problem description
I am getting an IndexError
when drawing a Choropleth using a bins
parameter as a sequence.
Sample of the stacktrace from code quoted above:
.../folium/features.py in color_scale_fun(x)
1007
1008 color_idx = np.digitize(value_of_x, bin_edges, right=False) - 1
-> 1009 return color_range[color_idx], fill_opacity
1010
1011 else:
IndexError: list index out of range
Drawing a choropleth with a provided sequence for bins is expected to work and not result in an error.
Expected Output
A correctly drawn map with choropleth according to the given bins
parameter is expected.
Output of folium.__version__
I was using the latest release 0.8.3, I cannot check this now as I am using my own edited version of the 0.8.0 branch where I “fixed” the issue.
Custom fix
I was able to get a correctly drawn map with bins
working as expected by cloning branch v.0.8.0 and changing line 1073 in folium/features.py
:
nb_bins = len(bin_edges) - 1
to
nb_bins = len(bin_edges)
Doing the same on the master
branch made my layer selection button disappear.
I didn’t have the time to check if this will break anything else, and hence would not want to create a PR with this change 😃
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top GitHub Comments
I will try fix it.
I have the same bug while working with Choropleth. And I am able to solve by this https://github.com/python-visualization/folium/issues/1130#issuecomment-504949715 So this issue hasn’t been solved yet.