Chart value for a specific color in a nautical chart

I am drawing a heatmap in Python with a sea tree package. The values ​​that I draw are discrete, these are integers -1 , 0 and 1 .

I would like the cells in the heatmap with a value of -1 show green, those with 0 yellow and 1 as red.

Can this rule be specified in the cubehelix_palette() or colour_palette() functions?

+6
source share
1 answer

You can use matplotlib ListedColormap as follows:

 import numpy as np import seaborn as sns from matplotlib.colors import ListedColormap data = np.random.randint(-1, 2, (10,10)) # Random [-1, 0, 1] data sns.heatmap(data, cmap=ListedColormap(['green', 'yellow', 'red']), annot=True) 

which gives:

enter image description here

You can replace the strings with 'green', 'yellow', 'red' hexadecimal colors such as '#FF0000' (equivalent to 'red' ) or rgb colors like (1.,0.,0.) (Also equivalent to 'red' ).

+11
source

All Articles