简体   繁体   中英

Seaborn heatmap total sum

Hi I have a seaborn heatmap and I would like to add a sum of each row to the right and a sum of each column at the bottom of my heatmap. Is this possible?

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

You can just take a sum of the confusion matrix along axes and stack it on to itself using numpy's stacking functions.

import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt

confusion_matrix = np.array([[417924,  67554],
                             [ 24901,  11070]])

confusion_matrix = np.hstack((confusion_matrix,confusion_matrix.sum(axis=1).reshape(-1,1)))
confusion_matrix = np.vstack((confusion_matrix,confusion_matrix.sum(axis=0).reshape(1,-1)))

x_axis_labels = ['predicted_non_churns','predicted_churns','col1+col2'] # labels for x-axis
y_axis_labels = ['actual_non_churns','actual_churns','row1+row2'] # 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

关联

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM