Python Seaborn Heatmap and Correlation Matrix
Use pandas to compute a correlation matrix and Seaborn to visualize it as a heatmap. Customize annotations and color scales, and handle large datasets efficiently.

When you need to understand relationships between numeric columns in a dataset, a correlation matrix heatmap is one of the most direct visualizations available. In Python, Seaborn's heatmap function combined with pandas' corr method lets you create one in a few lines of code.
Computing the Correlation Matrix with pandas
Before drawing anything, you need the correlation matrix itself. pandas provides the corr method on DataFrames:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv("sales_data.csv")
numeric_df = df.select_dtypes(include=["number"])
corr_matrix = numeric_df.corr()The corr method defaults to Pearson correlation and returns a square DataFrame where both rows and columns are the numeric columns of the original data. Each cell contains the correlation coefficient between the corresponding pair of columns, ranging from -1 to 1.
The select_dtypes(include=["number"]) filter is important. If your DataFrame contains object or datetime columns, corr will silently drop them, but being explicit about which columns are included makes the analysis reproducible and prevents surprises when the schema changes.
Drawing the Basic Seaborn Heatmap
With the correlation matrix ready, the heatmap is a single function call:
plt.figure(figsize=(10, 8))
sns.heatmap(corr_matrix, annot=True, cmap="coolwarm", center=0)
plt.title("Correlation Matrix Heatmap")
plt.tight_layout()
plt.show()The annot=True parameter places the correlation coefficient inside each cell. The cmap parameter selects the color map; coolwarm is a common choice because its divergent colors emphasize the sign of the correlation. Setting center=0 ensures that zero correlation maps to the midpoint of the color scale, so positive and negative values are visually balanced rather than skewed toward one end of the colormap.
Controlling Annotations, Color Scale, and Cell Appearance
The default annotation format is .2g, which uses two significant digits. If you prefer a fixed number of decimal places, use fmt:
sns.heatmap(corr_matrix, annot=True, fmt=".2f", cmap="viridis", vmin=-1, vmax=1)fmt=".2f" formats each coefficient to two decimal places. Setting vmin=-1 and vmax=1 pins the color scale to the theoretical range of correlation coefficients, which keeps colors consistent across different matrices. This is useful when comparing multiple heatmaps side by side, because the same color always represents the same coefficient value.
The square=True parameter forces each cell to be square, which makes the heatmap easier to read when the matrix is symmetric:
sns.heatmap(corr_matrix, annot=True, fmt=".2f", square=True, linewidths=0.5)The linewidths parameter adds a thin gap between cells, which improves readability when the matrix is dense.
Masking the Upper Triangle for Dense Matrices
A correlation matrix is symmetric: the coefficient for column A versus column B is identical to column B versus column A. Showing both halves is redundant and makes a large matrix harder to scan. You can mask the upper triangle using numpy:
import numpy as np
mask = np.triu(np.ones_like(corr_matrix, dtype=bool), k=1)
sns.heatmap(corr_matrix, mask=mask, annot=True, fmt=".2f", cmap="coolwarm", center=0)The mask is a boolean array where True cells are not drawn. np.triu with k=1 marks everything above the diagonal as True, leaving the diagonal and lower triangle visible. This removes the redundant upper half of the grid and focuses attention on the unique information in the matrix.
Handling Non-Numeric Columns and Missing Values
The corr method ignores non-numeric columns by default, but relying on that implicit behavior is risky. The select_dtypes approach shown earlier explicitly filters to numeric columns, which is safer when your DataFrame contains mixed types.
Missing values are handled by pandas' corr method using pairwise deletion: each pair of columns is compared using only the rows where both values are present. This means different pairs may be computed over different subsets of rows. If your dataset has substantial missing data, consider whether pairwise deletion is appropriate for your analysis, or fill missing values before computing correlations so the matrix is based on a consistent set of observations.
Performance and Memory Considerations for Large Matrices
The correlation computation is pairwise across columns, so its cost grows as O(n²) in the number of columns, where n is the column count. For datasets with hundreds of columns, this becomes noticeable. The heatmap rendering also scales with the number of cells: a 200 × 200 matrix has 40,000 cells, and rendering annotations for every cell is slow and visually unreadable.
For large matrices, annotations become impractical. Omitting them with annot=False is usually the right call when the matrix exceeds roughly 20 columns. Masking the upper triangle reduces visual clutter without losing information because the matrix is symmetric. When saving to a vector format, rasterized=True renders heatmap cells as a raster image while keeping text as vector; this cuts file size noticeably for large matrices.
Common Pitfalls and How to Avoid Them
One frequent issue is passing a DataFrame with non-numeric columns directly to heatmap. A correlation matrix must be numeric, so verify it before plotting, either with select_dtypes or by checking corr_matrix.dtypes.
Another issue is forgetting to call plt.savefig() or plt.show() in scripts. In non-interactive environments, the figure will not be displayed unless plt.show() is called, and it will not be saved unless plt.savefig() is called.
A third issue is using a colormap without setting center or vmin/vmax. The default color scale may not center on zero, making positive and negative correlations appear asymmetrically colored. Setting center=0 or explicit bounds keeps the visual encoding honest and prevents misleading interpretations of the color scale.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.