In this tutorial, we will look at how to create heatmaps in Python with the help of some examples.
What are Heatmaps?
Heatmaps use colour changes like hue, saturation, or brightness to depict the data as 2-D coloured maps. Instead of using numbers to represent relationships between variables, heatmaps use colours.
On both axes, these variables are plotted. The intensity of the colour in a given block determines the relationship between two values, and this relationship is described by the colour changes.
We can plot Heatmaps in different ways, they are:
- Using the
seaborn
library’sheatmap()
method. - Using the
matplotlib.pyplot.pcolormesh()
method. - Using the
matplotlib.pyplot.imshow()
method.
Let’s now look at the above methods in detail.
Heatmap the seaborn.heatmap()
method
In this method, we are going to use seaborn.heatmap()
function to create a heatmap. The following is the syntax –
Basic 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)
Parameters:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
data: 2D dataset that can be coerced into an ndarray. If a Pandas DataFrame is provided, the index/column information will be used to label the columns and rows.
For more details, about the parameter, refer this.
Now, let us understand the above method, with some examples.
Example 1 – Simple heatmap
# Import Modules import numpy as np import seaborn as sns # Generate a 10x10 random integer matrix data = np.random.rand(10,10) # Plot the heatmap sns.heatmap(data)
Output:

The steps followed in the above example are:
- import the required modules
- generate a 10*10 grid with random values
- plot a heatmap using
seaborn.heatmap
method.
Example 2 – Heatmap with customizations
You can further customize the heatmap with additional arguments to the seaborn.heatmap()
function. For example, let’s increase the line width in the heatmap and add annotations.
# 1. Import Modules import numpy as np import seaborn as sns # 2. Generate a 10x10 random integer matrix data = np.random.rand(10,10) # 3. Plot the heatmap sns.heatmap(data, linewidth = 1, annot=True)
Output:

The steps followed in the above example are:
- import the required modules
- generate a 10*10 grid with random values
- plot a heatmap using
seaborn.heatmap
method along with customizations.
Heatmap using the matplotlib.pyplot.pcolormesh()
method
In this method, we use the matplotlib.pyplot.pcolormesh
function to create a heatmap. The following is the syntax –
Basic Syntax:
matplotlib.pyplot.pcolormesh([X, Y,] C, **kwargs)
Parameters:
- C: The color-mapped values. Color-mapping is controlled by cmap, norm, vmin, and vmax.
- X, Y: The coordinates of the corners of quadrilaterals of a pcolormesh
For more details, about the parameters, refer this.
Now let us try to understand the above method with some examples.
Example 1 – Simple heatmap
Let’s draw the same heatmap as above (heatmap of a random 10×10 grid of values).
import matplotlib.pyplot as plt import numpy as np #Generate a 10x10 random integer matrix data= np.random.rand(10,10) #plotting the heatmap plt.pcolormesh(data) plt.show()
Output:

The steps followed in the above example are:
- import the required modules
- generate a 10*10 grid with random values
- plot a heatmap using
pyplot.pcolormesh
method.
Example 2 – Heatmap with customizations
You can further customize the heatmap with additional arguments to the matplotlib.pyplot.pcolormesh()
function. For example, we could change the color map using the cmap
parameter.
Let’s plot the same heatmap of different subplots with different cmap
values.
import matplotlib.pyplot as plt import numpy as np #Generate a 10x10 random integer matrix data= np.random.rand(10,10) #plotting the heatmap plt.subplot(2,2,1) plt.pcolormesh(data, cmap = 'rainbow') #plotting the heatmap plt.subplot(2,2,2) p1 =plt.pcolormesh(data, cmap = 'twilight') #plotting the heatmap plt.subplot(2,2,3) plt.pcolormesh(data, cmap = 'summer') #plotting the heatmap plt.subplot(2,2,4) plt.pcolormesh(data, cmap = 'winter') plt.tight_layout() plt.show()
Output:

The steps followed in the above example are:
- import the required modules
- generate a 10*10 grid with random values
- plot a heatmap using
pyplot.pcolormesh
method in different subplots.
To know more about the cmap
argument, refer this.
Heatmap using the matplotlib.pyplot.imshow()
method
In this method, we use matplotlib.pyplot.imshow
method to create a heatmap. The imshow()
method is used to display data as an image. The following is the syntax –
Basic Syntax:
matplotlib.pyplot.imshow(X, cmap=None, norm=None, *, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, interpolation_stage=None, filternorm=True, filterrad=4.0, resample=None, url=None, data=None, **kwargs)
Parameters:
- X: The image data. Supported array shapes are:
(M, N): an image with scalar data. The values are mapped to colors using normalization and a colormap. See parameters norm, cmap, vmin, vmax.
(M, N, 3): an image with RGB values (0-1 float or 0-255 int).
(M, N, 4): an image with RGBA values (0-1 float or 0-255 int), i.e. including transparency.
The first two dimensions (M, N) define the rows and columns of the image.
For more details, about the parameters, refer this.
Now let us understand the above method using some examples.
Example 1 – Simple heatmap
Let’s draw the same heatmap for the data used in the above methods (a random 10×10 grid of values) using the maplotlib.pyplot.imshow()
method.
import numpy as np import matplotlib.pyplot as plt #Generate a 10x10 random integer matrix data= np.random.random((10,10)) #plotting the heatmap plt.imshow( data, interpolation = 'nearest',cmap="twilight") plt.show()
Output:

The steps followed in the above example are:
- import the required modules
- generate a 10*10 grid with random values
- plot a heatmap using
plt.imshow
method.
Example 2 – Heatmap with customizations
We can also further customize the heatmap with additional arguments to the matpltolib.pyplot.imshow()
function. For example, let’s change the colormap in the above heatmap and draw different heatmaps on subplots with different colormaps.
import numpy as np import matplotlib.pyplot as plt #Generate a 10x10 random integer matrix data= np.random.random((10,10)) #plotting the heatmap plt.subplot(2,2,1) plt.imshow( data, interpolation = 'nearest',cmap="rainbow") plt.subplot(2,2,2) plt.imshow( data, interpolation = 'nearest',cmap="twilight") plt.subplot(2,2,3) plt.imshow( data, interpolation = 'nearest',cmap="summer") plt.subplot(2,2,4) plt.imshow( data, interpolation = 'nearest',cmap="ocean") plt.tight_layout() plt.show()
Output:

The steps followed in the above example are:
- import the required modules
- generate a 10*10 grid with random values
- plot a heatmap using
pyplot.imshow
method in different subplots.
To know more about the cmap
argument, refer this.
You might also be interested in –
- How to Create a Contour Plot in Matplotlib
- How To Make a Bubble Plot in Python with Matplotlib?
- Matplotlib – Create a Plot with two Y Axes and shared X Axis
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.