Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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=-1innp.tri()excludes the diagonalk=0includes the diagonal in the trianglemask=Truehides those elements in the heatmapinterpolation="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.
