如何将 Xlabel 移动到顶部

3 machine-learning pandas

有什么办法可以将 xLabel(predicted label) 0, 1, 移动到混淆矩阵的顶部吗?感谢任何帮助。

from sklearn.metrics import accuracy_score
plt.figure(figsize=(6,4))
sns.heatmap(cm_rf, annot=True, fmt="d")
plt.title('Random Forest Tree \nAccuracy:{0:.3%}\n'.format(accuracy_score(test_y, predict_rf_y)))
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
Run Code Online (Sandbox Code Playgroud)

我的输出

小智 7

您正在寻找的属性是 'ax.xaxis.set_ticks_position('top')' & 'ax.xaxis.set_label_position('top')':

f, ax = plt.subplots(figsize=(6, 4))
sns.heatmap(np.random.randint(0, 3, size=(2, 2)), annot=True, fmt="d")
ax.set_title('Random Forest Tree \nAccuracy:{format}\n', y=1.08)
ax.set_ylabel('True label')
ax.set_xlabel('Predicted label')

ax.xaxis.set_ticks_position('top')
ax.xaxis.set_label_position('top')
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助