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’s`heatmap()`

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:**

**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.**