Python
pandas
data visualization
heatmap
matplotlib

Making heatmap from pandas DataFrame

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A heatmap is one of the fastest ways to spot patterns in matrix-like data such as correlations, cohort metrics, or category comparisons. In Pandas workflows, most effort goes into shaping the table correctly before plotting. Once the matrix is correct, Seaborn and Matplotlib can render clean, production-ready heatmaps with only a few lines.

Build a Basic Heatmap from a Numeric DataFrame

Start with a rectangular numeric DataFrame where rows and columns have clear business meaning.

python
1import pandas as pd
2import seaborn as sns
3import matplotlib.pyplot as plt
4
5sales = pd.DataFrame(
6    {
7        "North": [120, 135, 128, 142],
8        "South": [95, 101, 99, 110],
9        "East": [130, 126, 139, 145],
10        "West": [88, 91, 93, 104],
11    },
12    index=["Q1", "Q2", "Q3", "Q4"],
13)
14
15plt.figure(figsize=(8, 4))
16sns.heatmap(sales, annot=True, fmt="d", cmap="YlGnBu", linewidths=0.5)
17plt.title("Quarterly Sales by Region")
18plt.tight_layout()
19plt.show()

This gives a readable baseline with numeric annotations and visible cell boundaries.

Convert Long Data to Matrix with Pivot

Real data often arrives in long format with one measurement per row. You need to pivot before plotting.

python
1import pandas as pd
2import seaborn as sns
3import matplotlib.pyplot as plt
4
5raw = pd.DataFrame(
6    {
7        "month": ["Jan", "Jan", "Feb", "Feb", "Mar", "Mar"],
8        "region": ["North", "South", "North", "South", "North", "South"],
9        "orders": [210, 185, 235, 176, 248, 193],
10    }
11)
12
13matrix = raw.pivot(index="month", columns="region", values="orders")
14
15plt.figure(figsize=(6, 4))
16sns.heatmap(matrix, annot=True, fmt="d", cmap="Blues")
17plt.title("Orders by Month and Region")
18plt.tight_layout()
19plt.show()

If there are duplicate keys, use pivot_table with an aggregation function such as mean or sum.

Handle Missing Data Intentionally

Missing values should not be silently plotted as zero unless that is semantically correct. Use masks or explicit fill rules.

python
1import numpy as np
2import seaborn as sns
3import matplotlib.pyplot as plt
4
5matrix2 = matrix.copy()
6matrix2.loc["Feb", "South"] = np.nan
7
8mask = matrix2.isna()
9
10plt.figure(figsize=(6, 4))
11sns.heatmap(
12    matrix2,
13    mask=mask,
14    annot=True,
15    fmt=".0f",
16    cmap="viridis",
17    cbar_kws={"label": "Orders"},
18)
19plt.title("Heatmap with Missing Value Mask")
20plt.tight_layout()
21plt.show()

Masking communicates data absence clearly and prevents misleading imputation.

Correlation Heatmap for Feature Analysis

Heatmaps are frequently used for correlation inspection during feature engineering.

python
1import pandas as pd
2import seaborn as sns
3import matplotlib.pyplot as plt
4
5features = pd.DataFrame(
6    {
7        "price": [10, 12, 13, 15, 18, 20],
8        "units": [100, 95, 90, 82, 75, 70],
9        "marketing": [5, 6, 7, 8, 9, 11],
10        "returns": [2, 2, 3, 3, 4, 4],
11    }
12)
13
14corr = features.corr(numeric_only=True)
15
16plt.figure(figsize=(5, 4))
17sns.heatmap(corr, annot=True, vmin=-1, vmax=1, center=0, cmap="coolwarm", square=True)
18plt.title("Feature Correlation")
19plt.tight_layout()
20plt.show()

Setting vmin, vmax, and center creates consistent color semantics for negative and positive relationships.

Improve Readability on Large Matrices

Large matrices become unreadable with default settings. Tune chart geometry and labels.

python
1plt.figure(figsize=(12, 8))
2ax = sns.heatmap(matrix, cmap="magma", cbar=True)
3ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha="right")
4ax.set_yticklabels(ax.get_yticklabels(), rotation=0)
5plt.tight_layout()
6plt.show()

For very large tables, consider plotting subsets, clustering, or interactive tools instead of one dense static heatmap.

Export for Reports and Dashboards

When charts are embedded in docs or dashboards, export with explicit resolution.

python
1plt.figure(figsize=(8, 4))
2sns.heatmap(sales, annot=True, cmap="YlOrRd")
3plt.title("Sales Heatmap")
4plt.tight_layout()
5plt.savefig("sales_heatmap.png", dpi=200)

Explicit dpi avoids blurry output in slide decks or wikis.

Practical Styling Guidelines

A few styling choices improve interpretability:

  • choose sequential palettes for magnitude-only metrics
  • choose diverging palettes for centered metrics
  • keep annotation precision aligned with metric scale
  • add meaningful axis labels and title

The goal is to reveal structure, not maximize visual effects.

Common Pitfalls

A common issue is passing non-numeric columns directly to heatmap, which can fail or produce confusing casts. Always select numeric data intentionally.

Another pitfall is using a diverging palette for strictly positive metrics, which implies a midpoint that does not exist.

Teams also annotate every cell in very large matrices. That reduces readability and slows rendering.

Missing value handling is frequently overlooked. Filling NaN with zero without domain justification can change interpretation.

Finally, inconsistent color ranges across multiple heatmaps makes side-by-side comparison misleading. Pin ranges when comparison matters.

Summary

  • Build a correct matrix first, then render the heatmap.
  • Use pivot or pivot_table for long-to-wide transformation.
  • Handle missing values with masks or explicit fill policy.
  • Match colormap choice to metric meaning and comparison goals.
  • Tune labels, size, and export settings for readable production output.

Course illustration
Course illustration

All Rights Reserved.