[英]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.