Creating Heatmap with Python

A heat map consists of values exhibiting different shades of one colour for individual values to be plotted. Generally, the darker shades of the chart represent values that are higher than the lighter shade. For a distinctly different value, a totally different colour can also be used. The data values are displayed with the help of colours in the graph. The main aim of a heatmap is to provide a coloured visual list of the given information. A heat map (or heatmap) is a data visualization technique that displays the magnitude of a phenomenon as colour in two dimensions. A heatmap, to be precise, is a technique of data visualization that makes use of colour in order to exhibit how a value of interest varies on the basis of the values of the two other variables. To sum it up, using different colours to represent data gives you a general view of the numerical data. In addition to this, Python heatmap includes executing cluster analysis, regularising the matrices, choosing a certain colour palette as well as commuting rows and columns to place values that are similar nearby. 

For instance, you could make use of a heatmap in order to understand how exactly air pollution changes according to the time of day across a set of towns and cities. 

A website heatmap can be understood in two ways: by seeing the visualization and by revising the raw data points. Click trends and issues can be noticed at a glance because of the colour-coded nature of heatmaps (red indicates the most interaction, blue the least). 

A 2-D Heatmap is a data visualization tool that allows the representation of the magnitude of the phenomenon in the form of colours. In Python, 2-D Heatmaps can be plotted using the Matplotlib package. There are several methods that can be employed to plot 2-D Heatmaps. some of them are discussed below. 

Check out our data science courses to upskill yourself.

Method 1: Using matplotlib.pyplot.imshow() Function 

Syntax: matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None,

vmax=None, origin=None, extent=None, shape=<deprecated parameter>, filternorm=1, filterrad=4.0, 

imlim=<deprecated parameter>, resample=None, url=None, \, data=None, \\*kwargs) 

Method 2: Using Seaborn Library 

For this we use seaborn.heatmap() function 

Syntax: seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, robust=False,annot=None, 

fmt=’.2g’, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, cbar_kws=None, cbar_ax=None, 

square=False, xticklabels=’auto’, yticklabels=’auto’, mask=None, ax=None, **kwargs) 

Explore our Popular Data Science Courses

Method 3: Using matplotlib.pyplot.pcolormesh() Function 

Syntax: matplotlib.pyplot.pcolormesh(*args, alpha=None, norm=None, cmap=None, vmin=None, vmax=None, 

shading=’flat’, antialiased=False, data=None, **kwargs) 

Seaborn is a Python library which enables us to create better charts with ease, with the help of its heatmap() function. This section begins with a post explaining the fundamental usage of the function based on any kind of data input. Then, it will guide you through the various ways to customize the chart, like controlling color and data normalization. 

Usually, we use some clustering techniques on a heatmap. This is done in order to group items having a similar sort of pattern for their numeric variables. 

Generally, it is recommended to display a dendrogram (A dendrogram is a diagram that shows the hierarchical relationship between objects. It is typically made in the form of output from hierarchical clustering. The principle function of a dendrogram is to find out the most suitable way to allocate objects to clusters.) on

top of the heatmap to describe how the clusterization has been carried out. Last but not least, it can be helpful to compare the grouping we got with an expected structure, shown as an additional color. 

How to interpret a heatmap in Python: 

Top Data Science Skills to Learn

Python Data Visualization — Heatmaps 

  1.  import pandas as pd. import numpy as np. import matplotlib.pyplot as plt. ...
  2.  fig, ax = plt.subplots(figsize=(10,6)) sns.heatmap(data.corr(), center=0, cmap='Blues') ax.set_title('Multi-Collinearity of Car Attributes') 3. fig, ax = plt.subplots(figsize=(10,6)) sns.heatmap(data.corr(), center=0, cmap='BrBG', annot=True)

Why Use Heatmaps?

In a heatmap in Python, a two-dimensional graphical representation of data, each value in a matrix is represented by a different colour.

With the help of the Matplotlib tools provided by the Seaborn package, annotated heatmaps can be created and customised to the creator’s specifications.

  • Heat Maps are, by definition, graphic representations of data that use colour-coding schemes. Heat Maps help users focus on the parts of data visualisations that matter most by helping to better visualise the volume of locations and events inside a dataset.
  • But heatmaps are much more than that. Many individuals are unaware that Heat Maps may be used for a few other data visualisations. Heat Maps are likely most frequently used to give a broader picture of quantitative numbers because they rely on colour to express values. This is particularly true when working with big data because colours make information easier to understand and distinguish from raw statistics. 

Heat Maps are inherently self-explanatory, unlike other types of data visualisation, which require interpretation from analysts or business users. Greater quantity is associated with deeper shades (tighter dispersion is associated with higher values, etc.). A heatmap correlation Python can showcase the strength of relationship between different variables through a plot.

How to create a heatmap: 

  1. Load the dataset.
  2. Make a Python Numpy array.
  3. Produce a Pivot in Python.
  4. Fashion an Array to Annotate the Heatmap.
  5. Build the Matplotlib figure and define the plot.
  6. Construct the Heatmap.

Now, the question is, how can you increase the size of the annotations of a the seaborn heatmap in Python? It’s simple – A seaborn can be explained in terms of a Python library that is based on matplotlib and is actually used for data visualization.

Read our popular Data Science Articles



  • Module to be imported
  • Load or produce data
  • Call heatmap () function with annot set to True.
  • Size has to be customized to annot_kws parameter
  • Plot to be exhibited

Changing heatmap colour:

The colour of seaborn heatmap can be changed by using the colour map’s cmap attribute of the heatmap. 

Types of Heatmaps: 

There are typically two types of Heatmaps: 

  1. Grid heatmap: There is a magnitude of values shown through colours that are laid out into a matrix of rows as well as columns, most commonly by a density-based function. Listed below are a few Grid heatmaps
  • Clustered heatmap – The sheer goal of clustered heatmap is to simply build associations between both the features as well as the data points. This is the sort of heatmap that implements clustering as part of the process of grouping features that are similar in nature.

Clustered heat maps are also widely used in biological sciences in order to study gene similarities across various individuals. 

  • Spatial heatmap – A heatmap has several squares and each square in a heatmap is actually assigned a colour representation on the basis of the nearby cells’ value. The very location of colour is based on the magnitude of the value in the particular space. These Heatmaps are actually data-driven paint by numbers canvas that is covered on top of an image. The cells that have a higher value than other cells are assigned a hot colour, while cells that have lower values are assigned a cold colour.

Check out all trending Python tutorial concepts in 2024

Other Uses of Heatmaps 

  • Business analytics: A heatmap is used as a visual business analytics tool and it provides really quick visual cues about the current performance, results as well as any scope for improvements. Heatmaps can also analyse the existing data as well as find areas of intensity that might reflect where most customers reside. Heatmaps can also be continually updated in order to reflect the growth and efforts. These maps can also be integrated into a business’ workflow and thereby become a part of ongoing analytics. These

present the data in a visually pleasing manner and also makes it easy to understand and communicate to team members or clients. 

  • Website: Heatmaps are actually used in websites in order to visualise data of visitors. This sort of visualization helps business owners, as well as marketers, identify the best as well as the worst-performing section of a certain webpage. These visions also help them optimize their website better. 
  • Exploratory Data Analysis: This is also known as EDA and it is a task that is performed by data scientists in order to get familiar with all the data. In fact, all the initial studies are done so as to understand the data known as EDA. It can also be explained as the process of analysing datasets before the modeling task begins. As a matter of fact, it is quite a boring task to look at a spreadsheet filled with numbers in order to determine important characteristics in a dataset. Thus, EDA is done in order to summarise their main features and specifications, often with visual methods, which also includes Heatmaps. These are a captivating way of visualising relationships between variables in a high dimensional space. This can be easily done by using feature variables as row headers as well as column headers. 
  • Molecular Biology: Heatmaps are used in order to study disparity as well as similarity patterns in RNA, DNA, etc. 
  • Geovisualization: Geospatial heat map charts are quite useful when it comes to displaying how geographical areas of a map are compared to each other on the basis of specific criteria. Heatmaps also help in cluster analysis or hotspot analysis in order to detect clusters of high concentrations of activity. Take, for instance, Airbnb rental price analysis!
  • Marketing & Sales: A heatmap’s capacity to detect cold, as well as warm spots, is used in order to advance marketing response rates by targeted marketing. Heatmaps also facilitate the detection of areas that respond to campaigns, under-served markets, customer residence as well as high sale trends – these help to enhance product lineups, capitalise on sales as well as build targeted customer segments while also analysing regional demographics. 

Basic Seaborn Heatmap Example

import numpy as npy

import seaborn as sbn

import matplotlib.pyplot as plt

data = npy.random.randint(low = 1, high = 100, size = (10, 10))

print(“To be plotted:\n”)


hm = sbn.heatmap(data = data)


To be plotted:

[[36 40 65 68 32 74 21 36 11 22]

[18 32 85 78 33 18 70 17 24 55]

[63 74 12 56 46 55 67 16 16 94]

[ 17 41 51 97  18 31 86 33 91 22]

[52 64  1 80 33 30 91 80 28 88]

[11 73 54 28 12 13 89 378 2  31]

[41 46 53 37 73 27 51 21 42 20]

[ 19 38 40 57 83 76 20 33 70  12]

[11 78 23 11  12 50 72 63 87 86]

[76 23 60 11 65 76 12 69 24 49]]


While there are many different colour schemes that can illustrate the heatmap, it also comes with a set of perceptual advantages as well as disadvantages for each. The colour palette choices actually go beyond just aesthetics, especially because the colours in the heatmap reveal patterns in the data. In fact, good colour schemes can enhance pattern discovery. However, poor choices can actually hide it. In addition to this, the seaborn heatmaps are the grid heat maps that can actually take various types of data in order to generate heatmaps. Therefore, the primary purpose of the seaborn heatmap is to simply show the correlation matrix by data visualization. It also helps in finding the relationship between multiple features as well as which features are best for machine learning model building.

If you’d want to dive deeper into working with Python, especially for data science, upGrad brings you the Executive PGP in Data Science. This program is designed for mid-level IT professionals, software engineers looking to explore Data Science, non-tech analysts, early career professionals, etc. Our structured curriculum and extensive support ensure our students reach their full potential without difficulties. 

You can also learn all Python Concepts in our Python Tutorials section.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks