How to Plot Confusion Matrix with Labels in Sklearn?
Last Updated : 14 Apr, 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 With LabelsStep 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
xticks
and 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:
Percentages to the confusion matrixFrom 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.Greens
to 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:
CM with Changing Color Maps3. 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:
CM with Titles and axis labelsNow our confusion matrix looks cleaner and more descriptive for presentations or reports.
Similar Reads
How to solve Error in Confusion Matrix
In this article, we will discuss what is Confusion Matrix and what are the causes of the error in a Confusion Matrix and How to solve an Error in a Confusion Matrix in R Programming Language. What is Confusion Matrix?A confusion matrix is an essential tool for assessing a model's performance in mach
3 min read
How to Make a Scatter Plot Matrix in R
A scatterplot matrix is ââa grid of scatterplots that allows us to see how different pairs of variables are related to each other. We can easily generate a scatterplot matrix using the pairs() function in R programming. In this article, we will walk through the process of creating a scatterplot matr
6 min read
How to set axes labels & limits in a Seaborn plot?
In this article, we will learn How to set axes labels & limits in a Seaborn plot. Let's discuss some concepts first. Axis is the region in the plot that contains the data space. The Axes contain two or three-axis(in case of 3D) objects which take care of the data limits.Axes Labels are the label
4 min read
How to Make ECDF Plot with Seaborn in Python?
Prerequisites: Â Seaborn In this article, we are going to make the ECDF plot with Seaborn Library. ECDF PlotECDF stands for Empirical Commutative Distribution. It is more likely to use instead of the histogram for visualizing the data because the ECDF plot visualizes each and every data point of the
5 min read
How To Align Kde Plot With Strip Plot In Seaborn?
A high-level interface for creating attractive and informative statistical graphics is offered by a powerful python library Seaborn. One of the most common tasks in data visualization is aligning different types of plots in one graph to gain insights into the data. In this article, we will understan
4 min read
How to Normalize a Confusion Matrix
A confusion matrix is a fundamental tool in classification problems, providing insight into the performance of a classification model. It displays the true positive, false positive, true negative, and false negative predictions, giving a detailed view of how well the model is performing across diffe
4 min read
How to change colorbar labels in matplotlib ?
In this article, we are going to see how to change color bar labels in matplotlib using Python. The colorbar() function is used to plot the color bar which belongs to the pyplot module of matplotlib adds a colorbar to a plot indicating the color scale. Syntax: matplotlib.pyplot.colorbar(mappable=Non
3 min read
How to Dump Confusion Matrix Using TensorBoard Logger in PyTorch Lightning
PyTorch Lightning is a lightweight wrapper for PyTorch that helps you scale your models and write less boilerplate code. TensorBoard, a popular visualization tool, can be integrated with PyTorch Lightning to monitor training processes. One valuable visualization is the confusion matrix, which provid
5 min read
How To Adjust Position of Axis Labels in Matplotlib?
Matplotlib is a powerful Python library for creating graphs and charts. By default, it places axis labels in standard positions, but sometimes you might want to move them for better readability or design. This article explains easy ways to adjust the position of axis labels in Matplotlib to make you
3 min read
How to Plot a Correlation Matrix into a Graph Using R
A correlation matrix is a table showing correlation coefficients between sets of variables. It's a powerful tool for understanding relationships among variables in a dataset. Visualizing a correlation matrix as a graph can provide clearer insights into the data. This article will guide you through t
4 min read