Heatmap

Python · Data Visualization

Theory

A heatmap shows a table of numbers as a grid of coloured cells, so large and small values stand out by colour. Its most common use in data work is the correlation matrix: df.corr() gives the Pearson correlation between every pair of numeric columns, from -1 (move in opposite directions) through 0 (no straight-line relationship) to 1 (move together), and the diagonal is always 1.

In matplotlib, ax.imshow(corr, cmap='RdBu_r', vmin=-1, vmax=1) colours the grid; fixing vmin and vmax keeps 0 at the neutral centre of the diverging colormap. Set the tick labels to the column names, write each value with ax.text in a loop and add fig.colorbar(im) as a key. seaborn's sns.heatmap(corr, annot=True, cmap='coolwarm') does all of this in one call. Heatmaps also suit pivot tables such as sales by city and month.

Why we use it

With many columns, a table of correlations is hard to scan. A heatmap shows at a glance which features are strongly related to the target or to each other, which guides feature selection and warns about duplicated information.

When to use it

Use a heatmap for correlation matrices, confusion matrices and two-way pivot tables (city x month). Use a diverging colormap with fixed -1 to 1 limits for correlations; for raw counts or amounts use a sequential colormap.

How it works

  1. DataFrame of numeric columns
  2. corr = df.corr() builds the matrix
  3. ax.imshow(corr, cmap='RdBu_r', vmin=-1, vmax=1)
  4. Label ticks with column names and write values with ax.text
  5. Any pair near +1 or -1? Yes: Strong relationship: check for redundancy. No: Weak or no linear link.
  6. Correlation heatmap

Syntax

corr = df.corr(numeric_only=True)
im = ax.imshow(corr, cmap='RdBu_r', vmin=-1, vmax=1)
ax.set_xticks(range(n)); ax.set_xticklabels(corr.columns)
fig.colorbar(im, ax=ax)
# seaborn: sns.heatmap(corr, annot=True)

Example

corr = df.corr(numeric_only=True)
fig, ax = plt.subplots()
im = ax.imshow(corr, cmap='RdBu_r', vmin=-1, vmax=1)
fig.colorbar(im, ax=ax)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic