Conditional Formatting For hv.HeatMap Colours

I’m unclear if you’re wanting separate cmaps for different conditions, or if you just want a single different color for the different conditions. If all you need is the latter, you can do:

green = '#00FF00'
amber = '#FFFF00'
red = '#FF0000'

Data = [['A', 'Foo', 0.2] , ['B', 'Bar', 0.9], ['C', 'Cat', 0.7]]
df = pd.DataFrame(Data, columns = ['Name', 'Category', 'Value'])

levels = [0, 0.7, 0.9, 1]
colors = [green, amber, red]

df_hm = hv.HeatMap(df, kdims=['Category','Name'], vdims=['Value'])
df_hm.opts(width=900, height=400, cmap=colors, color_levels=levels, tools=['hover'])

A similar question was asked here: Holoviews Heatmap specify color for each point - #5 by iRave

I’d guess that the reason why “the order of the colours does not match up with the data at all, and appears random” when you passed a list of colors to cmap before is because you were setting the color by the “colors” column, which is a string, and thus ordered according to string sorting rules.

If you need to be able to map separate cmaps altogether for each condition, then I think you’ll need a more complex solution, but I’d be happy to give it a go.

1 Like