数据是我乱编的~,根据这个数据来画混淆矩阵的热力图。你只要换数据就行。
#加载包
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Assuming you have a 2D array
data = np.array([[80, 21, 68, 2, 7, 10, 12, 1, 2, 7],
[9, 91, 16, 1, 6, 12, 4, 24, 1, 8],
[44, 21, 67, 3, 40, 3, 16, 1, 3, 2],
[0, 5, 4, 92, 7, 24, 23, 13, 2, 11],
[2, 1, 36, 5, 80, 15, 11, 2, 35, 13],
[1, 18, 11, 13, 8, 41, 26, 0, 6, 14],
[7, 5, 35, 66, 21, 23, 90, 1, 5, 7],
[0, 1, 2, 1, 3, 26, 6, 90, 2, 13],
[3, 2, 13, 0, 44, 1, 63, 3, 91, 12],
[0, 7, 3, 5, 10, 7, 10, 10, 9, 99]])
fig, ax = plt.subplots()
heatmap = ax.imshow(data, cmap='cool', interpolation='nearest') #你可以在cmap=这里改变热力图的颜色
# 在每个图像单元格的中心位置添加数值标注
for i in range(len(data)):
for j in range(len(data[i])):
text = ax.text(j, i, data[i][j],
ha='center', va='center', color='black')
plt.colorbar(heatmap) #数据条
plt.show()
#你可以在cmap=这里改变热力图的颜色
关于cmap:
链接: matplotlib.pyplot——cmap直观理解