Apply Masks To Seaborn Heatmap in Python

Masks in Seaborn heatmaps are used to highlight or hide certain parts of your data.

You can apply masks if you want to focus on specific data points, compare certain segments, or simplify your visualizations.

In this tutorial, you’ll learn various techniques for applying masks to Seaborn heatmaps.

From basic upper and lower triangle masks to row/column-specific and threshold-based masks.

 

 

Applying Upper Triangle Mask

First, let’s import the necessary libraries and create some sample data:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
data = np.random.rand(10, 10)

This code generates a 10×10 matrix of random numbers.

Now, let’s create an upper triangle mask:

mask = np.triu(np.ones_like(data, dtype=bool))
sns.heatmap(data, mask=mask)
plt.show()

Output:

Applying Upper Triangle Mask

Here, np.triu is used to create a boolean mask for the upper triangle of the matrix, which is then applied to the heatmap.

 

Applying Lower Triangle Mask

Let’s create a lower triangle mask:

mask = np.tril(np.ones_like(data, dtype=bool))
sns.heatmap(data, mask=mask)
plt.show()

Output:

Applying Lower Triangle Mask

In this snippet, np.tril is used to generate a boolean mask for the lower triangle of the matrix.

 

Row or Column Specific Mask

This method is useful when you need to highlight or compare specific sections of your data matrix without the distraction of the remaining data.

mask = np.full_like(data, True, dtype=bool)
mask[2:5, 3:7] = False
sns.heatmap(data, mask=mask)
plt.show()

Output:

Row or Column Specific Mask

In this code, np.full_like creates a mask filled with True (indicating masked areas), and then specific rows and columns are set to False (unmasked).

The result is a heatmap where only a specified rectangle (rows 2-4 and columns 3-6) is visible, and the rest is masked.

 

Exclude a column from formatting

To exclude a column from being formatted, you can manipulate the data parameter or use masking techniques.

You can apply a mask to the specific column to prevent it from being formatted.

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = {
    'Call Duration': [10, 15, 12, 13],
    'Data Usage': [200, 230, 180, 190],
    'Customer Satisfaction': [3, 4, 2, 5],
    'Signal Strength': [80, 70, 85, 90]
}
df = pd.DataFrame(data)
corr_matrix = df.corr()

# Mask to exclude a specific column from formatting
# For instance, to exclude 'Signal Strength', create a mask for this column
mask = np.zeros_like(corr_matrix, dtype=bool)
mask[:, -1] = True  # Adjust the index as per the column to exclude
sns.heatmap(corr_matrix, mask=mask, annot=True, fmt=".2f")
plt.show()

Output:

Exclude a column from formatting

 

Threshold Based Mask

Applying a threshold-based mask to a Seaborn heatmap is a powerful technique for focusing on data points that exceed a specific threshold.

This method is useful when you want to highlight elements that are either above or below a certain value. Let’s walk through how to implement this.

For this example, let’s set a threshold and create a mask based on it:

threshold = 0.7
mask = data < threshold
sns.heatmap(data, mask=mask)
plt.show()

Output:

Threshold Based Mask

This code first defines a threshold (0.7 in this case) and then creates a boolean mask where every element in the data that is lower than the threshold is marked True (to be masked).

 

Custom Mask (Individual Cells)

Creating a custom mask for individual cells in a Seaborn heatmap allows for precise control over which data points are displayed.

This method allows you to highlight or exclude specific data points based on custom criteria.

Let’s create a custom mask for individual cells:

mask = np.full_like(data, True, dtype=bool)
selected_cells = [(0, 0), (1, 2), (3, 3), (4, 7)]
for cell in selected_cells:
    mask[cell] = False
sns.heatmap(data, mask=mask)
plt.show()

Output:

Custom Mask

In this script, np.full_like is used again to create a full mask. Then, specific cells are unmasked by setting them to False based on the selected_cells list.

Leave a Reply

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