Skip to content Skip to sidebar Skip to footer

Confusionmatrixdisplay (scikit-learn) Plot Labels Out Of Range

The following code plots a confusion matrix: from sklearn.metrics import ConfusionMatrixDisplay confusion_matrix = confusion_matrix(y_true, y_pred) target_names = ['aaaaa', 'bbbbb

Solution 1:

I think the easiest way would be to switch into tight_layout and add pad_inches= something.

from sklearn.metrics import confusion_matrix
from sklearn.metrics import ConfusionMatrixDisplay
import matplotlib.pyplot as plt
from numpy.random import default_rng

rand = default_rng()
y_true = rand.integers(low=0, high=7, size=500)
y_pred = rand.integers(low=0, high=7, size=500)


confusion_matrix = confusion_matrix(y_true, y_pred)
target_names = ["aaaaa", "bbbbbb", "ccccccc", "dddddddd", "eeeeeeeeee", "ffffffff", "ggggggggg"]
disp = ConfusionMatrixDisplay(confusion_matrix=confusion_matrix, display_labels=target_names)
disp.plot(cmap=plt.cm.Blues, xticks_rotation=45)

plt.tight_layout()
plt.savefig("conf.png", pad_inches=5)

Result:

Confusion matrix where all text in the axes is visible.

Post a Comment for "Confusionmatrixdisplay (scikit-learn) Plot Labels Out Of Range"