Calculate RGB value for a range of values to create heat map

前端 未结 4 1763
故里飘歌
故里飘歌 2020-12-04 13:01

I am trying to create a heat map with python. For this I have to assign an RGB value to every value in the range of possible values. I thought of changing the color from blu

4条回答
  •  一向
    一向 (楼主)
    2020-12-04 13:23

    You can often eliminate an if with an index into an array of two values. Python lacks a ternary conditional operator, but this works:

    r = [red_curve_1, red_curve_2][value>=halfmax]
    g = [green_curve_1, green_curve_2][value>=halfmax]
    b = [blue_curve_1, blue_curve_2][value>=halfmax]
    

    Replace the *_curve_1 and *_curve_2 expressions with the constants or slopes or curves either left or right of the midpoint, respectively.

    I'll leave those substitutions to you, but for example:

    • red_curve_1 and blue_curve_2 are simply 0
    • green_curve_1 is 255*(value-minimum)/(halfmax-minimum)
    • etc.

提交回复
热议问题