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.

`IndexError` in Choropleth when providing `bins` as a sequence.

See original GitHub issue

Firstly, 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
LeonardoFurtadocommented, Jun 28, 2019

I will try fix it.

0reactions
muneebablecommented, Jun 7, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I create a folium.chloropleth map without an index error?
Try to place a float in line_opacity so 1.0 instead of 1. – Steven · Oh to clarify- the error is thrown on...
Read more >
Choropleth() Got An Unexpected Keyword Argument 'Bins' On ...
I am getting an IndexError when drawing a Choropleth using a bins parameter as a sequence. Sample of the stacktrace from code quoted...
Read more >
Choropleth Maps - A Guide to Data Classification
The quantile map tries to bin the same count of features in each of the 5 classes. In other words, quantile maps try...
Read more >
Changelog — GeoPandas 0.12.2+0.gefcb367.dirty ...
Fix display of lower bin in legend label of choropleth plots using a PySAL scheme (#450). Version 0.2.0#. Improvements: Complete overhaul of the...
Read more >
How to step up your Folium Choropleth Map skills
The first step is to clean the data to plot our Choropleth map. ... In order to add this functionality, we will again...
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