[英]Labelling both percentages and absolute values on the cells in seaborn heatmap confusion matrix
我尝试在热图的单元格上同时写入百分比和绝对值,例如“15 (20%)”。 我可以通过下面的代码计算每个 class 的百分比。 但是,我怎样才能为两者做到这一点?
h_map = sns.heatmap(pyArray_cf / np.sum(pyArray_cf, axis=1), vmin=0, vmax=1, annot=True, cmap="Blues",
ax=ax1, fmt='.2%',
mask=pyArray_cf==0,
xticklabels=x_axis_labels_1,
yticklabels=y_axis_labels_1)
另外,我想用 0 值(0 次混淆)来掩盖单元格。
感谢特伦顿评论中提到的一些要点,我已经建立了下面的热图。 希望这是您正在寻找的。
div
获取每个单元格的百分比。 通过将每个单元格除以整个列的总和来计算百分比。 假设这是你需要的data = np.random.rand(10, 12) #Random data 0 to 1
df = pd.DataFrame(data)
#Create perc to store the percentages for each column using DIV.
perc = df.copy()
cols=perc.columns.values
perc[cols]=perc[cols].div(perc[cols].sum(axis=1), axis=0).multiply(100)
#Use annot to store the text value to display in each cell. Number \n Percent % format
annot=df.round(2).astype(str) + "\n" + perc.round(1).astype(str) + "%"
#Create plot - adjust size, linewidth and provide annotation text for display
fig, ax = plt.subplots(figsize=(10,8))
sns.heatmap(data, annot=annot, fmt='', vmin=0, vmax=1, cmap="Blues",
annot_kws={"fontsize":8}, linewidth=1, ax=ax)
Plot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.