Plotting only the upper/lower triangle of a heatmap in Matplotlib

To plot only the upper or lower triangle of a heatmap in Matplotlib, we can use NumPy to create a mask that hides specific parts of the data. This technique is useful for correlation matrices where the upper and lower triangles contain identical information.

Plotting Lower Triangle Heatmap

We use numpy.tri() to create a triangular mask and apply it to our data −

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create random 5x5 data
data = np.random.rand(5, 5)

# Create mask for lower triangle (k=-1 excludes diagonal)
mask = np.tri(data.shape[0], k=-1)

# Apply mask to data
masked_data = np.ma.array(data, mask=mask)

# Create heatmap
plt.imshow(masked_data, interpolation="nearest", cmap='copper')
plt.colorbar()
plt.title("Lower Triangle Heatmap")
plt.show()

Plotting Upper Triangle Heatmap

To show only the upper triangle, we invert the mask using logical NOT −

import numpy as np
import matplotlib.pyplot as plt

# Create random correlation-like data
np.random.seed(42)
data = np.random.rand(5, 5)

# Make it symmetric (like a correlation matrix)
data = (data + data.T) / 2
np.fill_diagonal(data, 1)

# Create mask for upper triangle (excluding diagonal)
mask = np.tri(data.shape[0], k=-1)

# Apply inverted mask to show upper triangle
masked_data = np.ma.array(data, mask=~mask.T)

plt.imshow(masked_data, interpolation="nearest", cmap='viridis')
plt.colorbar()
plt.title("Upper Triangle Heatmap")
plt.show()

Using Seaborn for Better Triangle Heatmaps

Seaborn provides more elegant triangle heatmap functionality −

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Create sample correlation data
np.random.seed(42)
data = np.random.randn(5, 5)
corr_matrix = np.corrcoef(data)

# Create masks
upper_mask = np.triu(np.ones_like(corr_matrix, dtype=bool))
lower_mask = np.tril(np.ones_like(corr_matrix, dtype=bool))

# Plot upper triangle
plt.figure(figsize=(8, 6))
sns.heatmap(corr_matrix, mask=upper_mask, annot=True, cmap='coolwarm', center=0)
plt.title("Lower Triangle Correlation Heatmap")
plt.show()

Comparison of Methods

Method Library Best For
np.tri() + imshow() Matplotlib + NumPy Simple triangle masks
sns.heatmap() with mask Seaborn Correlation matrices with annotations
np.triu()/np.tril() NumPy More control over triangle selection

Key Parameters

  • k=-1 in np.tri() excludes the diagonal

  • k=0 includes the diagonal in the triangle

  • mask=True hides those elements in the heatmap

  • interpolation="nearest" creates sharp edges between cells

Conclusion

Use np.tri() with masked arrays for basic triangle heatmaps in Matplotlib. For correlation matrices with annotations, Seaborn's heatmap() with masks provides better visualization and built-in features like colorbars and value annotations.

Updated on: 2026-03-25T21:11:56+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements