首先是热力图(heatmap),直接用matplotlib画就行了,不需要使用seaborn,seaborn应该就是用的matplotlib的代码,要修改东西还不如直接用matplotlib方便,另外本人在画热力图图时遇到,用matplotlib3.0出现画出的图会边缘的地方只显示半格,改成matplotlib2.0就正确了。
'''
以上省略数据处理,注意数据的shape必须是n*n
'''
fig, ax = plt.subplots()
im = ax.imshow(L2) #imshow即画热力图
cbar=ax.figure.colorbar(im, ax=ax) #设置旁边显示的bar
# We want to show all ticks...
Pruning_step = [9, 8, 7, 6, 5, 4, 3, 2, 1] #坐标刻度值
gamma = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
ax.set_xticks(np.arange(9))
ax.set_yticks(np.arange(9))
# ... and label them with the respective list entries
ax.set_yticklabels(Pruning_step) #设置坐标刻度值
ax.set_xticklabels(gamma)
valfmt=matplotlib.ticker.StrMethodFormatter('{x:.3f}') #给热力图标注文本设置格式
# Loop over data dimensions and create text annotations.
for i in range(9):
for j in range(9):
text = ax.text(j, i, valfmt(L2[i, j], None)