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

[Improvement] Asymmetric colormaps #34

Open
cristi-neagu opened this issue Jul 1, 2019 · 5 comments
Open

[Improvement] Asymmetric colormaps #34

cristi-neagu opened this issue Jul 1, 2019 · 5 comments
Milestone

Comments

@cristi-neagu
Copy link

Hello,

Would be very useful to have a built in function that returns asymmetric colormaps. Something that would take a min, a max, and a center, and would return a colormap that conforms to that. This would be useful when the aim is for the colormap to be centered about 0.

@jsignell
Copy link
Member

Just as a follow up, the min, max and center would be colors and numbers right?

@cristi-neagu
Copy link
Author

The way i see it, it should be a mapping between values and a 0 to 1 normalized interval which represents the colormap range. So, if i want to plot some values that fall between -20 and +50, and i am using a diverging colormap and i want to keep the white representing a value of 0 rather than +15, i'd do something maybe like this:

# using a dictionary type declaration:
rescale_cmap({-20 : 0, 0 : 0.5, 50 : 1}, cc.coolwarm)

# or maybe a list would be better:
rescale_cmap(input=[-20, 0, 50], output=[0, 0.5, 1], cmap=cc.coolwarm)

Any values outside the specified interval (-20, 50) would be clamped to the interval for the purposes of color mapping. If the center is outside the (min, max) interval, the range would be clamped to the center value and the opposite end of the interval. So if (center < min) then min = center.

This would be suitable for my use case. I don't know how general or particular my use case is.

Thank you.

@jbednar
Copy link
Member

jbednar commented Jul 15, 2019

If you use a Bokeh colormap, in Colorcet that's just a list of length 256, so you should be able to do this type of manipulation without any additional tools. E.g. if you want an asymmetric map, with the negative range being half the positive range, just drop the bottom 25% of the colormap (e.g. something like fire[64:]. You should be able to write a rescale_cmap function that converts from normalized values to these integer list positions, and if that's useful we can put that into colorcet (though it wouldn't be specific to colorcet objects, just any Bokeh palette). Doing it for Matplotlib colormaps would be a bit more work, as those aren't just Python lists.

@cristi-neagu
Copy link
Author

@jbednar Thanks for the tip, didn't know it's that easy.

Would be good to have a standardised, core method of doing this for any colormap, hence the suggestion.

@jbednar
Copy link
Member

jbednar commented Jul 15, 2019

Sounds great; we're happy to accept a PR for rescale_cmap that works on both Bokeh and matplotlib formats!

@jbednar jbednar added this to the wishlist milestone Oct 5, 2020
@jbednar jbednar removed the wishlist label Oct 5, 2020
@jbednar jbednar added wishlist and removed wishlist labels Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants