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

Floating point error in plotting.bokeh.colormap_generator #4911

Closed
palmhjell opened this issue Apr 27, 2021 · 0 comments · Fixed by #4934
Closed

Floating point error in plotting.bokeh.colormap_generator #4911

palmhjell opened this issue Apr 27, 2021 · 0 comments · Fixed by #4934
Milestone

Comments

@palmhjell
Copy link

I came across a subtle bug in the way plotting.bokeh.colormap_generator normalizes colormaps with the default arguments, such that something like hv.Palette(f'Category{N}', samlples=N) will not return every color in the colormap. Example here for N=10:

image

Two pinks (#e377c2) are returned and gray (#7f7f7f) is missing.

This can be fixed by adding a very small term to the upper value of the range argument (e.g., (0, 1+1e-10)), but is mainly related to floating point error in what plotting.bokeh.colormap_generator returns:

image

Clearly that value is supposed to be 7 but int(6.9999...) is 6.

I played around with other types of creating that integer (e.g., round), but the best method I found that always keeps the same expected behavior for all colormaps and also fixes these float errors is to just add a small term in the colormap_generator function:

def colormap_generator(palette):
    # Account for potential floating point error
    error = 1e-10
    return lambda value: palette[int(value*(len(palette)-1)+error)]

This fixes the issue and returns the expected behavior:
image

I've got a fork with this fixed and can open a PR, but it's honestly a 1/2-line fix, so I figured an actual dev could just make that change easily enough.

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.

2 participants