VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/how-to-plot-confusion-matrix-with-labels-in-sklearn/

⇱ How to Plot Confusion Matrix with Labels in Sklearn? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

Step 2: Generating Predictions

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

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.

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

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.

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.

Output:

πŸ‘ download-(77)
CM with Titles and axis labels

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

Comment