plotly-calplot icon indicating copy to clipboard operation
plotly-calplot copied to clipboard

How to use colorscale?

Open Nemecsek opened this issue 1 year ago • 1 comments

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?

Nemecsek avatar Jan 30 '24 09:01 Nemecsek

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.

msz0ke avatar Mar 28 '24 19:03 msz0ke