Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Seaborn · Function

heatmap

seaborn.heatmap displays a rectangular two-dimensional dataset as a color-encoded matrix. It can add annotations, cell borders, labels, masking, and a colorbar.

How it works

heatmap renders a rectangular two-dimensional dataset as a matrix whose cell colors encode values. It operates on the active Matplotlib Axes unless you provide ax, and it normally reserves part of that Axes for a colorbar.

Syntax

seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt='.2g', annot_kws=None, linewidths=0, linecolor='white', cbar=True, cbar_kws=None, cbar_ax=None, square=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, **kwargs)

Parameters

NameTypeDescription
datarectangular datasetRectangular two-dimensional dataset that can be converted to an array; DataFrame labels are used for rows and columns.
vmin, vmaxfloat, optionalLower and upper values anchoring the colormap; inferred when omitted.
cmapmatplotlib colormap name or object, optionalColormap name or Matplotlib colormap object.
centerfloat, optionalValue at which to center the colormap.
robustbool, optionalIf true, compute color limits using robust quantiles rather than extremes.
annotbool or rectangular dataset, optionalWhether to annotate cells, or a separate rectangular annotation dataset.
fmtstr, optionalFormatting string for annotation text.
annot_kwsdict, optionalKeyword arguments passed to annotation text.
linewidths, linecolorfloat, str, optionalWidth and color of lines separating cells.
cbar, cbar_kws, cbar_axbool, dict, matplotlib Axes, optionalWhether to draw a colorbar and options controlling it.
squarebool, optionalWhether to force square cells.
xticklabels, yticklabelsbool, int, list, or auto, optionalControls column and row tick labels.

Returns

matplotlib Axes. The function draws the heatmap on the supplied or active Axes.

Key points

  • Use heatmap to visualize a rectangular matrix by encoding each cell's value with color.
  • It integrates with Matplotlib's current Axes when no ax argument is given.
  • vmin and vmax explicitly set the lower and upper values used for the color mapping.
  • center can alter the default colormap when no cmap is explicitly selected.
  • Set annot to display numeric values or other annotations inside the heatmap cells.
  • Use linewidths and linecolor to visually separate neighboring cells.

Common mistakes

  • A colorbar is enabled by default and consumes part of the plotting Axes; disable it with cbar=False or supply cbar_ax.
  • A DataFrame's index and columns provide row and column labels, unlike a plain array.

Example: Annotated matrix heatmap

import seaborn as sns
import matplotlib.pyplot as plt

values = [[1, 2, 3], [4, 5, 6]]
sns.heatmap(values, annot=True, fmt="d", cmap="crest")
plt.show()

This draws a numeric matrix with cell values displayed as annotations and uses the named crest colormap.

When to use it

Matrix pattern exploration

Inspect numeric tables where magnitude and patterns matter more than exact values.

Correlation matrix visualization

Display correlations with a diverging palette, centered scale, and optional mask.

Annotated tabular display

Show values directly inside cells with controlled formatting.

Interview questions

QWhat input does heatmap expect?

The function accepts a rectangular two-dimensional dataset and maps its values to colors. A DataFrame also contributes its index and column labels to the plot.

Follow-up: How would you show values in each cell? Pass annot=True, then use fmt to control their formatting.

QHow do you annotate a heatmap?

annot=True writes the cell values as text, while fmt controls the formatting string used for those annotations. You can also pass a separate rectangular dataset through annot.

Follow-up: How can annotation text be styled? Supply a dictionary through annot_kws.

QHow does heatmap handle Axes and the colorbar?

heatmap is an Axes-level function, so it draws into the current Axes when ax is omitted. It creates a colorbar by default unless cbar is false or a separate cbar_ax is supplied.

Follow-up: How can the color scale be bounded? Set vmin and vmax to anchor the colormap limits.

  • clustermap
  • set_theme
  • load_dataset
  • cubehelix_palette
  • diverging_palette

Source: seaborn.heatmap. Checked 2026-10-15.