brunorosilva / plotly-calplot

The easiest and best looking Calendar Heatmap you'll find, made with Plotly.
https://pypi.org/project/plotly-calplot/
104 stars 10 forks source link

How to use colorscale? #31

Open Nemecsek opened 8 months ago

Nemecsek commented 8 months ago

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 commented 6 months ago

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.