The Wayback Machine - https://web.archive.org/web/20241125225702/https://www.geeksforgeeks.org/seaborn-heatmap-a-comprehensive-guide/
Open In App

Seaborn Heatmap – A comprehensive guide

Last Updated : 12 Nov, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix. In this, to represent more common values or higher activities brighter colors basically reddish colors are used and to represent less common or activity values, darker colors are preferred. Heatmap is also defined by the name of the shading matrix. Heatmaps in Seaborn can be plotted by using the seaborn.heatmap() function.

seaborn.heatmap()

Syntax: seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, **kwargs)

Important Parameters:

  • data: 2D dataset that can be coerced into an ndarray.
  • vmin, vmax: Values to anchor the colormap, otherwise they are inferred from the data and other keyword arguments.
  • cmap: The mapping from data values to color space.
  • center: The value at which to center the colormap when plotting divergent data.
  • annot: If True, write the data value in each cell.
  • fmt: String formatting code to use when adding annotations.
  • linewidths: Width of the lines that will divide each cell.
  • linecolor: Color of the lines that will divide each cell.
  • cbar: Whether to draw a colorbar.

All the parameters except data are optional.

Returns: An object of type matplotlib.axes._subplots.AxesSubplot 

Let us understand the heatmap with examples.

Basic Heatmap

Making a heatmap with the default parameters. We will be creating a 10×10 2-D data using the randint() function of the NumPy module.

Python3




# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
  
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low = 1,
                         high = 100,
                         size = (10, 10))
print("The data to be plotted:\n")
print(data)
  
# plotting the heatmap
hm = sn.heatmap(data = data)
  
# displaying the plotted heatmap
plt.show()


Output:

The data to be plotted:

[[46 30 55 86 42 94 31 56 21  7]
[68 42 95 28 93 13 90 27 14 65]
[73 84 92 66 16 15 57 36 46 84]
[ 7 11 41 37  8 41 96 53 51 72]
[52 64  1 80 33 30 91 80 28 88]
[19 93 64 23 72 15 39 35 62  3]
[51 45 51 17 83 37 81 31 62 10]
[ 9 28 30 47 73 96 10 43 30  2]
[74 28 34 26  2 70 82 53 97 96]
[86 13 60 51 95 26 22 29 14 29]]

We’ll be using this same data in all the examples.

Anchoring the colormap

If we set the vmin value to 30 and the vmax value to 70, then only the cells with values between 30 and 70 will be displayed. This is called anchoring the colormap.

Python3




# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
  
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
                         high=100,
                         size=(10, 10))
  
# setting the parameter values
vmin = 30
vmax = 70
  
# plotting the heatmap
hm = sn.heatmap(data=data,
                vmin=vmin,
                vmax=vmax)
  
# displaying the plotted heatmap
plt.show()


Output:

Choosing the colormap

In this, we will be looking at the cmap parameter. Matplotlib provides us with multiple colormaps, you can look at all of them here. In our example, we’ll be using tab20.

Python3




# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
  
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
                         high=100,
                         size=(10, 10))
  
# setting the parameter values
cmap = "tab20"
  
# plotting the heatmap
hm = sn.heatmap(data=data,
                cmap=cmap)
  
# displaying the plotted heatmap
plt.show()


Output:

Centering the colormap

Centering the cmap to 0 by passing the center parameter as 0.

Python3




# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
  
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
                         high=100,
                         size=(10, 10))
  
# setting the parameter values
cmap = "tab20"
center = 0
  
# plotting the heatmap
hm = sn.heatmap(data=data,
                cmap=cmap,
                center=center)
  
# displaying the plotted heatmap
plt.show()


Output:

Displaying the cell values

If we want to display the value of the cells, then we pass the parameter annot as True. fmt is used to select the datatype of the contents of the cells displayed. 

Python3




# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
  
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
                         high=100,
                         size=(10, 10))
  
# setting the parameter values
annot = True
  
# plotting the heatmap
hm = sn.heatmap(data=data,
                annot=annot)
  
# displaying the plotted heatmap
plt.show()


Output:

Customizing the separating line

We can change the thickness and the color of the lines separating the cells using the linewidths and linecolor parameters respectively.

Python3




# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
  
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
                         high=100,
                         size=(10, 10))
  
# setting the parameter values
linewidths = 2
linecolor = "yellow"
  
# plotting the heatmap
hm = sn.heatmap(data=data,
                linewidths=linewidths,
                linecolor=linecolor)
  
# displaying the plotted heatmap
plt.show()


Output:

Hiding the colorbar

We can disable the colorbar by setting the cbar parameter to False.

Python3




# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
  
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
                         high=100,
                         size=(10, 10))
  
# setting the parameter values
cbar = False
  
# plotting the heatmap
hm = sn.heatmap(data=data,
                cbar=cbar)
  
# displaying the plotted heatmap
plt.show()


Output:

Removing the labels

We can disable the x-label and the y-label by passing False in the xticklabels and yticklabels parameters respectively.

Python3




# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
  
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
                         high=100,
                         size=(10, 10))
  
# setting the parameter values
xticklabels = False
yticklabels = False
  
# plotting the heatmap
hm = sn.heatmap(data=data,
                xticklabels=xticklabels,
                yticklabels=yticklabels)
  
# displaying the plotted heatmap
plt.show()


Output:



Previous Article
Next Article

Similar Reads

Mastering Heatmap Customization: Enhancing Heatmap Readability with Seaborn
Heatmaps are a powerful data visualization tool that allows you to represent data in a matrix format using colors. They are particularly useful for identifying patterns, correlations, and outliers in large datasets. Seaborn, a Python data visualization library based on Matplotlib, provides a simple and efficient way to create heatmaps. However, one
5 min read
Animating Seaborn's Heatmap : Step-by-Step Guide
Heatmaps and correlation matrices are powerful tools for visualizing data relationships and patterns. However, static heatmaps may not always capture the dynamic nature of data changes over time. Animating a heatmap or correlation matrix can provide deeper insights into how data evolves. This article will guide you through the process of creating a
4 min read
Seaborn Kdeplot – A Comprehensive Guide
Kernel Density Estimate (KDE) Plot is a powerful tool for estimating the probability density function of continuous or non-parametric data. KDE plot is implemented through the kdeplot function in Seaborn. This article explores the syntax and usage of kdeplot in Python, focusing on one-dimensional and bivariate scenarios for efficient data visualiza
4 min read
Comprehensive Guide to Adding Titles and Customizing Seaborn Boxplots
Seaborn is a powerful Python visualization library built on top of Matplotlib, providing a high-level interface for drawing attractive and informative statistical graphics. One common task when creating visualizations is adding titles to plots to make them more informative and easier to understand. This article will guide you through the process of
3 min read
Highlighting the Largest Bar in Seaborn Barplots: A Comprehensive Guide
Seaborn is a powerful Python visualization library based on Matplotlib that provides a high-level interface for drawing attractive and informative statistical graphics. One common task when visualizing data is to highlight specific data points to draw attention to them. In this article, we will explore how to set a different color to the largest ba
4 min read
Changing Plot Colors in Seaborn : A Comprehensive Guide
Seaborn is a powerful Python library for data visualization built on top of Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. One of the key features of Seaborn is its ability to handle color palettes effectively, which can significantly enhance the readability and aesthetics of your plots.
6 min read
Adding Titles to Seaborn Heatmaps: A Comprehensive Guide
Seaborn is a popular Python library for creating attractive and informative statistical graphics. Heatmaps are a versatile visualization tool used to represent matrix-like data, such as correlation matrices or confusion matrices. Adding a title to a Seaborn heatmap can provide context and improve the interpretability of the visualization. This arti
3 min read
Finding the Seaborn Version: A Comprehensive Guide
Finding the version of a Python package is a common task for developers and data scientists. Knowing the exact version of a package can help in debugging, ensuring compatibility, and maintaining reproducibility in projects. This article will guide you through various methods to find the version of the Seaborn library installed on your system. Table
3 min read
Hierarchically-clustered Heatmap in Python with Seaborn Clustermap
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas. What is Clustering? Clustering is basically groupin
2 min read
How to add a frame to a seaborn heatmap figure in Python?
A heatmap is a graphical representation of data where values are depicted by color. They make it easy to understand complex data at a glance. Heatmaps can be easily drawn using seaborn in python. In this article, we are going to add a frame to a seaborn heatmap figure in Python. Syntax: seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, cent
4 min read
How to add text in a heatmap cell annotations using seaborn in Python ?
Prerequisite: Seaborn heatmap Heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix. In this, to represent more common values or higher activities brighter colors basically reddish colors are used and to represent less common or activity values, darker colors are preferred. Heatmap is also define
3 min read
How to increase the size of the annotations of a seaborn heatmap in Python?
Prerequisites: Seaborn Seaborn is a Python library that is based on matplotlib and is used for data visualization. It provides a medium to present data in a statistical graph format as an informative and attractive medium to impart some information. A heatmap is one of the components supported by seaborn where variation in related data is portrayed
3 min read
How to change the colorbar size of a seaborn heatmap figure in Python?
Prerequisites: Seaborn A colorbar is a rectangular color scale that is used to interpret the data of a heatmap. By default, it is of the same size as the heatmap but its size can be changed using the cbar_kws parameter of the heatmap() function. This parameter accepts dictionary type values and to change the size of the colorbar, its shrink paramet
2 min read
How to create a Triangle Correlation Heatmap in seaborn - Python?
Seaborn is a Python library that is based on matplotlib and is used for data visualization. It provides a medium to present data in a statistical graph format as an informative and attractive medium to impart some information. A heatmap is one of the components supported by seaborn where variation in related data is portrayed using a color palette.
4 min read
Custom Color Palette Intervals in Seaborn Heatmap
Heatmaps are a powerful visualization tool for representing data in a matrix format, where individual values are represented by colors. Seaborn, a popular Python data visualization library, provides a convenient way to create heatmaps. However, customizing the color palette intervals in a Seaborn heatmap can significantly enhance the interpretabili
5 min read
Assigning Colors to Values in a Seaborn Heatmap
Heatmaps are a powerful visualization tool that can help you understand complex data sets by representing values as colors. The primary element of heatmeaps is the use of color. The right color palette can highlights subtle difference in the data, emphasize the relationships and make the heatmap more accessible and insightful. Seaborn can supports
7 min read
Expressing Classes on the Axis of a Heatmap in Seaborn
Heatmaps are a powerful tool in data visualization, allowing users to quickly identify patterns and relationships within a dataset. One crucial aspect of creating informative heatmaps is effectively expressing classes on the axis. This article will delve into the techniques and best practices for representing classes on the axis of a heatmap using
5 min read
How to Make Heatmap Square in Seaborn FacetGrid
Seaborn is a powerful Python visualization library based on Matplotlib that provides a high-level interface for drawing attractive and informative statistical graphics. One of the common visualizations used in data analysis is the heatmap, which shows data in a matrix form where each cell is colored according to its value. Sometimes, when using Fac
2 min read
Set Max Value for Color Bar on Seaborn Heatmap
Seaborn is a powerful Python visualization library that builds on Matplotlib and provides a high-level interface for drawing attractive statistical graphics. One of its most popular features is the heatmap function, which allows users to visualize data in a matrix format with color gradients. Sometimes, you may want to control the maximum value of
5 min read
How to create a seaborn correlation heatmap in Python?
Seaborn is a Python library that is based on matplotlib and is used for data visualization. It provides a medium to present data in a statistical graph format as an informative and attractive medium to impart some information. A heatmap is one of the components supported by seaborn where variation in related data is portrayed using a color palette.
3 min read
A Comprehensive Guide to 15 Essential Function for List Manipulation
In the world of Python programming, understanding and using list functions is like having a versatile toolbox at your disposal. Lists, which are flexible structures for storing data, are used in many situations, and knowing how to manipulate them with these functions is crucial. In this article, we will explore essential Python List functions that
4 min read
Python Mock Library: A Comprehensive Guide to Effective Testing
Welcome to the ultimate guide on mastering Python's Mock Library! If you're a Python developer looking to level up your testing skills, you've come to the right place. In this comprehensive guide, we will dive deep into the Mock Library, exploring its various features and learning how to use them effectively in your testing workflows. Testing is an
8 min read
Streamlining Data Cleaning with PyJanitor: A Comprehensive Guide
Data cleaning is a crucial step in the data analysis pipeline. It involves transforming raw data into a clean dataset that can be used for analysis. This process can be time-consuming and error-prone, especially when dealing with large datasets. PyJanitor is a Python library that aims to simplify data cleaning by providing a set of convenient funct
8 min read
Display the Pandas DataFrame in Heatmap style
Pandas library in the Python programming language is widely used for its ability to create various kinds of data structures and it also offers many operations to be performed on numeric and time-series data. By displaying a panda dataframe in Heatmap style, the user gets a visualisation of the numeric data. It gives an overview of the complete data
6 min read
3D Heatmap in Python
Heatmaps are a great way to visualize a dataset, methods for visualizing the data are getting explored constantly and 3D heatmap is one of the ways to plot data. Let's learn how we can plot 3D data in python. We are going to use matplotlib and mplot3d to plot the 3D Heatmap in Python. We need to install the matplotlib explicitly by running the foll
5 min read
How to draw 2D Heatmap using Matplotlib in python?
In this article, we will explain about plotting heatmaps using the matplotlib library. A heatmap is a great tool for visualizing data across the surface. It highlights data that have a higher or lower concentration in the data distribution. Heatmap A 2-D Heatmap is a data visualization tool that helps to represent the magnitude of the matrix in for
4 min read
Generate a Heatmap in MatPlotLib Using a Scatter Dataset
Heatmaps are a powerful visualization tool that can help you understand the density and distribution of data points in a scatter dataset. They are particularly useful when dealing with large datasets, as they can reveal patterns and trends that might not be immediately apparent from a scatter plot alone. In this article, we will explore how to gene
5 min read
Step-by-Step Guide to Annotating Diagonal Elements in Seaborn Heatmaps
Heatmaps are a powerful visualization tool used to represent data in a matrix format, where individual values are represented by colors. Seaborn, a Python data visualization library based on Matplotlib, provides a simple and intuitive way to create heatmaps. However, there are times when you might want to highlight or annotate only specific element
6 min read
A Step-by-Step Guide to Changing Plot Colors in Seaborn
Seaborn is a powerful Python library for data visualization built on top of Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. One of the key features of Seaborn is its ability to handle color palettes effectively, which can significantly enhance the readability and aesthetics of your plots.
5 min read
Adding a Legend to a Seaborn Point Plot: A Technical Guide
Seaborn is a powerful Python library for creating informative and attractive statistical graphics. One of its many capabilities is creating point plots, which are useful for visualizing relationships between variables. Adding a legend to a Seaborn point plot can enhance its interpretability, helping viewers understand the data and the categories be
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg