[英]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.嗨,我有一个 seaborn 热图,我想在右侧添加每行的总和,并在热图底部添加每列的总和。 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.您可以沿轴取混淆矩阵的总和,并使用 numpy 的堆叠函数将其堆叠到自身上。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.