Home > Blockchain >  Changing annotation text color in Seaborn heat map
Changing annotation text color in Seaborn heat map

Time:05-10

I would like to change the color of the annotation text in a seaborn heatmap. By default the text in darker areas of the plot is white. However, I am trying to match the style of another plot so I would like the text to be black.

I have attached an example of my code below

ax = sns.heatmap(test, annot=True, 
            fmt='.2', cmap= custom_color_map)

ax.set_title('Seaborn Confusion Matrix with labels\n\n');
ax.set_xlabel('Predicted Values')
ax.set_ylabel('Actual Values ');

## Ticket labels - List must be in alphabetical order
ax.xaxis.set_ticklabels(['0','1', '2', '3, '4'])
ax.yaxis.set_ticklabels(['0','1', '2', '3, '4'])

enter image description here

As you can see the text on the diagonal is white because the cells are darker. I would like to change that to black.

Thank you for your assistance.

CodePudding user response:

You can define your proper style in the annot_kws argument on heatmap function, and it's like in html.

Here's an example:

sns.heatmap(data.corr(), cmap=”coolwarm”, annot=True, annot_kws={‘fontsize’:12, ‘fontstyle’:’italic’, ‘color’:”b”, ‘alpha’:0.6, ‘rotation’:”vertical”,’verticalalignment’:’center’, ‘backgroundcolor’:’w’})

You can find more infos here in the vii. annot_kws part.

  • Related