How to Plot Confusion Matrix with Labels in Sklearn?

Last Updated : 23 Jul, 2025

A confusion matrix is a table used to evaluate the performance of a classification algorithm. It compares the actual target values with those predicted by the model.. This article will explain us how to plot a labeled confusion matrix using Scikit-Learn. Before go to the implementation let's understand the components of a confusion matrix:

  • True Positives (TP): Correctly predicted positive instances.
  • True Negatives (TN): Correctly predicted negative instances.
  • False Positives (FP): Number of instances incorrectly predicted as positive.
  • False Negatives (FN): Number of instance incorrectly predicted as negative.

This matrix is useful for calculating performance metrics like accuracy, precision, recall and F1-score

Visualizing Confusion Matrix with Labels

To get started, we need to have Python installed on your system along with the necessary libraries. We can install Scikit-Learn and Matplotlib using pip:

pip install scikit-learn matplotlib

Step 1: Building a Classification Model

Let's start by building a simple classification model. For this example, we'll use the Iris dataset which is included in Scikit-Learn. We load the Iris dataset split it into training and testing sets then train a Random Forest classifier on the training data.

Python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

iris = load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

clf = RandomForestClassifier(random_state=42)
clf.fit(X_train, y_train)

Step 2: Generating Predictions

Next we'll use the trained model to make predictions on the test set.

Python
y_pred = clf.predict(X_test)

Step 3: Plotting the Confusion Matrix with Labels

We create the confusion matrix and plot it using Scikit-Learn’s ConfusionMatrixDisplay with class names and a blue color map.

Python
cm = confusion_matrix(y_test, y_pred)

disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=iris.target_names)
disp.plot(cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.show()

Output:

confusion-matrix
Confusion Matrix With Labels

Step 4: Customizing the Confusion Matrix Plot

We can customize the confusion matrix plot to make it more informative and visually appealing.

1. Adding Percentages

We can add percentages to the confusion matrix to make it easier to interpret. It is then normalized by dividing each row by its total, which gives the percentage of predictions for each true class.

  • imshow() displays the normalized matrix as an image where each cell’s color intensity reflects the percentage value.
  • The xticksand yticks are labeled using the target class names to make the matrix more understandable.
  • The text color is automatically set to white or black based on the background color intensity for better visibility.
  • Finally tight_layout()adjusts spacing and the matrix is displayed with plt.show().
Python
cm = confusion_matrix(y_test, y_pred)

cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

fig, ax = plt.subplots()
im = ax.imshow(cm_normalized, interpolation='nearest', cmap=plt.cm.Blues)
ax.figure.colorbar(im, ax=ax)

ax.set(xticks=np.arange(cm.shape[1]),
       yticks=np.arange(cm.shape[0]),
       xticklabels=iris.target_names, yticklabels=iris.target_names,
       title='Normalized Confusion Matrix',
       ylabel='True label',
       xlabel='Predicted label')

plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
         rotation_mode="anchor")

fmt = '.2f'
thresh = cm_normalized.max() / 2.
for i in range(cm.shape[0]):
    for j in range(cm.shape[1]):
        ax.text(j, i, format(cm_normalized[i, j], fmt),
                ha="center", va="center",
                color="white" if cm_normalized[i, j] > thresh else "black")
fig.tight_layout()
plt.show()

Output:

normalized-confusion-matrix
Percentages to the confusion matrix

From the above image the diagonal values show how well the model correctly predicted each class. A value of 1.00 (or 100%) on the diagonal means perfect prediction for that class, while lower values indicate some misclassification.

2. Changing Color Maps

We can change the color map to suit our preferences. We change the color map to green using cmap=plt.cm.Greensto make the plot visually different.

Python
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=iris.target_names)
disp.plot(cmap=plt.cm.Greens)
plt.title('Confusion Matrix with Greens Color Map')
plt.show()

Output:

Greens-color-map
CM with Changing Color Maps

3. Adding Titles and Axis Labels

We can add titles and axis labels to make the plot more descriptive. We add a custom title and axis labels to clearly indicate what the plot represents.

Python
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=iris.target_names)
disp.plot(cmap=plt.cm.Blues)
plt.title('Confusion Matrix for Iris Dataset')
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.show()

Output:

download-(77)
CM with Titles and axis labels

Now our confusion matrix looks cleaner and more descriptive for presentations or reports.

Comment