corianne1234
corianne1234

Reputation: 724

Seaborn heatmap integer formatting

I have a heatmap plot with integers showing. As the numbers are quite high I was wondering if it is possible to write k instead of 000? So 417924 would become 418k.

confusion_matrix = ([[417924,  67554],
       [ 24901,  11070]])

x_axis_labels = ['predicted_non_churns','predicted_churns'] # labels for x-axis
y_axis_labels = ['actual_non_churns','actual_churns'] # labels for y-axis

ax = plt.axes()
ax.set_title('Confusion Matrix',fontsize=14, fontweight='bold')

sn.heatmap(confusion_matrix, annot=True, cmap="Purples",  
           xticklabels=x_axis_labels, yticklabels=y_axis_labels, fmt='g', ax=ax) # font size

Thanks a lot

Upvotes: 0

Views: 2172

Answers (1)

busybear
busybear

Reputation: 10590

You can define your own annotation with the annot parameter. Create a separate array with 'k' and apply it to your heatmap. You'll need to set fmt to '' as well:

rnd = np.round(confusion_matrix/1000).astype(int)
annot = np.char.add(rnd.astype(str), 'k')
sns.heatmap(confusion_matrix, annot=annot, fmt='', cmap="Purples",  
            xticklabels=x_axis_labels, yticklabels=y_axis_labels, ax=ax)

Upvotes: 1

Related Questions