Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
How to set axes labels & limits in a Seaborn plot?
Next article icon

How to Plot Confusion Matrix with Labels in Sklearn?

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
How to set axes labels & limits in a Seaborn plot?

D

devendrakayande427
Improve
Article Tags :
  • Machine Learning
  • Blogathon
  • AI-ML-DS
  • AI-ML-DS With Python
  • Data Science Blogathon 2024
  • Sklearn
Practice Tags :
  • Machine Learning

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences