Seaborn.heatmap 不根据特定值调整颜色
Seaborn.heatmap doesn't adjust colors based on specific value
我想要一个混淆矩阵的热图,它根据每个 class 的百分比生成颜色。例如,最高百分比变为黑色,其他百分比根据它们的百分比变亮(更高的百分比 = 更深的颜色)。我尝试更改 vmin
和 vmax
,但颜色会根据 'counts' 值而不是 grouped_percentages
.
进行更改
categories = ['a', 'b', 'c']
group_percentages = []
counts = []
for i in range (len(cf)):
for j in range(len(cf)):
group_percentages.append(cf[j,i]/np.sum(cf[:,i]))
counts.append(cf[j,i])
group_percentages = ['{0:.2%}'.format(value) for value in
group_percentages]
counts = ['{0:0.0f}'.format(value) for value in
counts]
labels = [f'{v1}\n{v2}' for v1, v2 in zip(group_percentages, counts)]
labels = np.asarray(labels).reshape(3,3,order='F')
sns.heatmap(cf, annot=labels, fmt='', xticklabels=categories, yticklabels=categories, cmap='Greys', vmax=100, cbar=False)
输出:
如您所见,虽然我将 vmax 设置为 100,但 cf[0,0]
为 100%,但热图中的颜色为灰色,但 cf[1,1]
为 89%,并且其颜色为黑色。
您应该使用 group_percentages
作为热图的数据,但首先您需要在 3x3 矩阵中重塑此列表:
percentages_matrix = np.reshape(group_percentages, (3, 3))
完整代码
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
cf = np.array([[23, 0, 3],
[0, 106, 5],
[0, 12, 76]])
categories = ['a', 'b', 'c']
group_percentages = []
counts = []
for i in range(len(cf)):
for j in range(len(cf)):
group_percentages.append(cf[j, i]/np.sum(cf[:, i]))
counts.append(cf[j, i])
percentages_matrix = np.reshape(group_percentages, (3, 3))
group_percentages = ['{0:.2%}'.format(value) for value in group_percentages]
labels = [f'{v1}\n{v2}' for v1, v2 in zip(group_percentages, counts)]
labels = np.asarray(labels).reshape(3, 3, order = 'F')
sns.heatmap(percentages_matrix, annot = labels, fmt = '', xticklabels = categories, yticklabels = categories, cmap = 'Greys', vmax = 1, vmin = 0, cbar = False)
plt.show()
我想要一个混淆矩阵的热图,它根据每个 class 的百分比生成颜色。例如,最高百分比变为黑色,其他百分比根据它们的百分比变亮(更高的百分比 = 更深的颜色)。我尝试更改 vmin
和 vmax
,但颜色会根据 'counts' 值而不是 grouped_percentages
.
categories = ['a', 'b', 'c']
group_percentages = []
counts = []
for i in range (len(cf)):
for j in range(len(cf)):
group_percentages.append(cf[j,i]/np.sum(cf[:,i]))
counts.append(cf[j,i])
group_percentages = ['{0:.2%}'.format(value) for value in
group_percentages]
counts = ['{0:0.0f}'.format(value) for value in
counts]
labels = [f'{v1}\n{v2}' for v1, v2 in zip(group_percentages, counts)]
labels = np.asarray(labels).reshape(3,3,order='F')
sns.heatmap(cf, annot=labels, fmt='', xticklabels=categories, yticklabels=categories, cmap='Greys', vmax=100, cbar=False)
输出:
如您所见,虽然我将 vmax 设置为 100,但 cf[0,0]
为 100%,但热图中的颜色为灰色,但 cf[1,1]
为 89%,并且其颜色为黑色。
您应该使用 group_percentages
作为热图的数据,但首先您需要在 3x3 矩阵中重塑此列表:
percentages_matrix = np.reshape(group_percentages, (3, 3))
完整代码
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
cf = np.array([[23, 0, 3],
[0, 106, 5],
[0, 12, 76]])
categories = ['a', 'b', 'c']
group_percentages = []
counts = []
for i in range(len(cf)):
for j in range(len(cf)):
group_percentages.append(cf[j, i]/np.sum(cf[:, i]))
counts.append(cf[j, i])
percentages_matrix = np.reshape(group_percentages, (3, 3))
group_percentages = ['{0:.2%}'.format(value) for value in group_percentages]
labels = [f'{v1}\n{v2}' for v1, v2 in zip(group_percentages, counts)]
labels = np.asarray(labels).reshape(3, 3, order = 'F')
sns.heatmap(percentages_matrix, annot = labels, fmt = '', xticklabels = categories, yticklabels = categories, cmap = 'Greys', vmax = 1, vmin = 0, cbar = False)
plt.show()