如何在图像热图中设置图例 (python)?

How to set a legend in an image heatmap (python)?

如何将垂直图例稍微向上移动 (到行的中心)? 我需要这些非常大的字母

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np


labels_raw=np.array([["98.75\n±0.50"  ,"1.25\n±0.82"],[ "1.32\n±0.74", "98.68\n±0.74"]])
cm_raw=np.array([[98.75  ,1.25],[ 1.32, 98.68]])

sns.set(font_scale=2.5)


labels=labels_raw
cm=cm_raw


fig, ax = plt.subplots(figsize=(5,5))

target_names = ['test1','test2'] 


f=sns.heatmap(cm, annot=labels, fmt=':^', xticklabels=target_names, 
          yticklabels=target_names,annot_kws={"size": 25},cbar=False)
plt.show(block=False)  
#plt.show()
fig=f.get_figure()

您可以旋转标签并将垂直对齐设置为居中:

fig, ax = plt.subplots(figsize=(5,5))

target_names = ['test1','test2'] 

sns.heatmap(cm, annot=labels, fmt=':^', xticklabels=target_names, 
          yticklabels=target_names,annot_kws={"size": 25},cbar=False,ax=ax)

ax.set_yticklabels(target_names, rotation=0, fontsize="25", va="center")

或者这将使 y 轴标签保持垂直:

sns.heatmap(cm, annot=labels, fmt=':^', xticklabels=target_names, 
          yticklabels=target_names,annot_kws={"size": 25},cbar=False,ax=ax)

ax.set_yticklabels(target_names, fontsize="25", va="center")