5

Is there any way I can move the xLabel(predicted label) 0, 1, to the top of the confusion matrix? Appreciate any help.

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()

My output

  • Possible duplicate of [Moving x-axis to the top of a plot in matplotlib](https://stackoverflow.com/questions/14406214/moving-x-axis-to-the-top-of-a-plot-in-matplotlib) – Space Impact Mar 22 '18 at 04:30

1 Answers1

14

The properties you're looking for are ax.xaxis.set_ticks_position('top') and ax.xaxis.set_label_position('top'). For example :

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')

Hope this helps

Soltius
  • 2,162
  • 1
  • 16
  • 28
TsurG
  • 246
  • 2
  • 4