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

color_brewer raises KeyError if user requests more colors than what the colormap has #104

Closed
alessioarena opened this issue Oct 31, 2021 · 0 comments · Fixed by #115
Closed

Comments

@alessioarena
Copy link

Problem
branca.utilities.color_brewer raises a non descriptive KeyError if the user requests more colors than the number of colors forming the selected colormap.

For example, RdYlBu is made of 11 individual colors. If the user requests more than 11 colors using the parameter n, this will trigger this error

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-8-32dd8f641e66> in <module>
      6 print(branca.utilities.color_brewer('RdYlBu', 11))
      7 
----> 8 print(branca.utilities.color_brewer('RdYlBu', 12))
      9 

~\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'

Minimal code

import branca
print(branca.__version__)
# 0.4.2

print(branca.utilities.color_brewer('RdYlBu'))
# ['#d73027', '#fc8d59', '#fee090', '#e0f3f8', '#91bfdb', '#4575b4']
# defaults to n=6

print(branca.utilities.color_brewer('RdYlBu', 11))
# ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee090', '#ffffbf', '#e0f3f8', '#abd9e9', '#74add1', '#4575b4', '#313695']

print(branca.utilities.color_brewer('RdYlBu', 12))
# KeyError

Other comments
This issue is linked to python-visualization/folium#1270

My understanding is that color_brewer is not meant to generate more colors than what is available. For that we can use branca.utilities.linear_gradient.
However, there are two issues I can see with this behaviour:

  • The KeyError raised is non descriptive and does not point to the real problem
  • I cannot see a straightforward way to know how many colors a specific colormap has, or to automatically return the maximum number of colors of that colormap
ajabep added a commit to ajabep/branca that referenced this issue Oct 3, 2022
The non-qualitative schemes should be extendable. This commit adds this feature.

Fixes python-visualization#104
Fixes python-visualization#114

Moreover, the following error was not reported via issues and are fixed:
* TypeError in the linear_gradient function when trying to extend any scheme. Indeed, in color_brewer, the key searched in the scheme database was not found, thus, it was passing `None` instead of a real scheme vector to linear_gradient;
* The scheme 'viridis' was not in the base_codes JSON;
* Multiple scheme hadn't any metadata in scheme_info JSON;
* When a `n` value provided to `color_scheme` was a float, it tried to select an unknown scheme without raising the right Exception type.
Conengmo pushed a commit that referenced this issue Nov 7, 2022
* Fix a bunch of errors in `color_brewer` and add unit tests

The non-qualitative schemes should be extendable. This commit adds this feature.

Fixes #104
Fixes #114

Moreover, the following error was not reported via issues and are fixed:
* TypeError in the linear_gradient function when trying to extend any scheme. Indeed, in color_brewer, the key searched in the scheme database was not found, thus, it was passing `None` instead of a real scheme vector to linear_gradient;
* The scheme 'viridis' was not in the base_codes JSON;
* Multiple scheme hadn't any metadata in scheme_info JSON;
* When a `n` value provided to `color_scheme` was a float, it tried to select an unknown scheme without raising the right Exception type.

* Fix deprecated `find_element_by_css_selector`

Selenium just removed that method in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

* Fix a forgotten incrementation
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.

1 participant