Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maximum bins allowed for different color brewer palettes, in choropleth? #1270

Closed
manubhatt3 opened this issue Mar 7, 2020 · 2 comments · Fixed by python-visualization/branca#115

Comments

@manubhatt3
Copy link

Please add a code sample or a nbviewer link, copy-pastable if possible

# Your code here

Problem description

Are there maximum no. of bins for different color brewer palettes in choropleth? Because if I am overriding the default 6 bins and setting it to 12, I am getting the message "Check to make sure that it is not a qualitative scheme." with some color brewer palettes.

Expected Output

Output of folium.__version__

@Conengmo
Copy link
Member

Conengmo commented Mar 9, 2020

Hi, could you share a bit more on what happened please. What was the full error? Can you share a short, self-contained code snippet that produces the same error?

@alessioarena
Copy link
Contributor

Hi,
I encountered the same issue.
Each ColorBrewer colormap has a certain set of colors, for example Accent is composed of 8 colors.
You can see the full list of colors here

If you are using a specific colormap and request more bins than the individual colors you will end up with a non descriptive KeyError raised in branca

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-5209f8da3457> in <module>
      1 import csiro_spatial as spatial
      2 data = 'test/RunDelivery/Step15 - RunEmberSimulation(LONG_TASK)/short_distance_embers_simulation_smoothed_23457de7a62.tif'
----> 3 spatial.plot.interactive(
      4     [
      5 #         (r"C:\House loss database\AireysInlet\test\RunDelivery\Input_data\CadastreParcels.shp", 'POSTCODE', 0.7, 'Reds'),

c:\house loss database\house_loss_model\libs\spatial\csiro_spatial\plot.py in interactive(data, provider, cmap, alpha, zoom, **kwargs)
    239                     # if 'bins' in kwargs:
    240                     #     colormap = _brew_colors(colormap, len(kwargs['bins']))
--> 241                     geo_j = folium.Choropleth(
    242                         geo_data=plot_d.to_json(),
    243                         data=plot_d,

~\Miniconda3\envs\houselossmodel\lib\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)
   1223             # We add the colorscale
   1224             nb_bins = len(bin_edges) - 1
-> 1225             color_range = color_brewer(fill_color, n=nb_bins)
   1226             self.color_scale = StepColormap(
   1227                 color_range,

~\Miniconda3\envs\houselossmodel\lib\site-packages\branca\utilities.py in color_brewer(color_code, n)
    150     if not explicit_scheme:
    151         # Check to make sure that it is not a qualitative scheme.
--> 152         if scheme_info[base_code] == 'Qualitative':
    153             matching_quals = []
    154             for key in schemes:

KeyError: 'RdYlBu'

The same error can be raised by doing

branca.utilities.color_brewer('RdYlBu', 30) #that colormap has only 11 colors

A workaround can be

branca.utilities.linear_gradient(branca.utilities.color_brewer('RdYlBu'), 30)

However there is no way that I can see to have color_brewer returning all available colors in that colormap unless you know how many there are. For example, branca.utilities.color_brewer('Accent') will return only the first 6 colors of Accent omitting the last two.
For this reason I reckon fixing this issue is better placed within branca more than folium

For the moment, and going forward I guess would be nice to be able to provide a list of hex colors of same length as bins when creating a Choropleth object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants