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

How to use colorscale? #31

Open
Nemecsek opened this issue Jan 30, 2024 · 1 comment
Open

How to use colorscale? #31

Nemecsek opened this issue Jan 30, 2024 · 1 comment

Comments

@Nemecsek
Copy link

Hi. Great library!

I need a clarification on how to use colorscale.
I have some data that I would like to color according to their value, not based on % of max as in the example.

In Medium's article you write "But of course, you may want to create your own color scale, there are several ways to create one, my favorite is actually to use a percentage of max values".
Are they described somewhere?

I would like to assign colors according the absolute value, not the % of max value:
i.e. red if below 5; yellow if below 10; white between 10 and 20; black if bigger than 20.
Absolute colors, no shading.

Is this possible?

@msz0ke
Copy link

msz0ke commented Mar 28, 2024

Hi. It is possible by creating a colorscale with color ranges to avoid interpolation,

     colorscale = [
        [0, "red"],
        [0.25, "red"],
        [0.25, "yellow"],
        [0.5, "yellow"],
        [0.5, "white"],
        [0.75, "white"],
        [0.75, "black"],
        [1, "black"],
    ]

then the values (your y parameter for the calplot) would need to be normalized to fit the colorscale.

   def normalize(value):
        if value < 5:
            return 0.125
        elif value < 10:
            return 0.375
        elif 10 <= value <= 20:
            return 0.625
        else:
            return 0.875

    df["value"] = df["value"].apply(normalize)

I don't think it's currently possible to keep the original values and use distinct colors without interpolation at the same time. You would need to able to modify the z parameter of the underlying go.Heatmap for that.

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

No branches or pull requests

2 participants