Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to heatmap plot using Seaborn

Fahad Farid

Seaborn is a popular Python-based data analysis toolkit that can be imported using:

import seaborn as sns

Seaborn can aid in the creation of multiple types of data analysis graphs. One such graph is the heatmap.

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)


  • data ** :rectangular dataset** - A 2D dataset that can be coerced into an array. If a Pandas DataFrame is provided, the index/column information will label the columns and rows.

  • vmin, vmax, : floats, optional - values to anchor the colormap; otherwise, they are inferred from the data and other keyword arguments.

  • cmap : matplotlib, colormap name or object, or list of colors, optional - The mapping from data values to color space. If not provided, the default will depend on whether the center is set.

  • center : float, optional -The value at which you need to center the colormap when plotting divergent data. Using this parameter will change the default cmap if none is specified.

  • robust : bool, optional - If True and vmin or vmax are absent, the colormap range will be computed with robust quantiles instead of the extreme values.

  • annot : bool or rectangular dataset, optional- If True, write the data value in each cell.

Note: DataFrames match on position, not index.

  • fmt :str, optional- The string formatting code to use when adding annotations.

  • annot_kws : dict of key, value mappings, optional - Keyword arguments for matplotlib.axes.Axes.text() when annot is True.

  • linewidths :float, optional - Width of the lines that will divide each cell.

  • linecolor :color, optional - Color of the lines that will divide each cell.

  • cbar :bool, optional - Whether or not to draw a color bar.

  • cbar_kws :dict of key, value mappings, optional - Keyword arguments for matplotlib.figure.Figure.colorbar().

  • cbar_ax :matplotlib Axes, optional - The axes in which to draw the color bar, otherwise take space from the main Axes.

  • square : bool, optional- If True, set the Axes aspect to “equal” so each cell will be square-shaped.

  • xticklabels, yticklabels : “auto”, bool, list-like, or int, optional - If True, plot the column names of the data frame. If False, don’t plot the column names. If list-like, plot these alternate labels as the xticklabels. If an integer, use the column names but only plot every n label. If “auto," try to densely plot non-overlapping labels.

  • mask :bool array or DataFrame, optional - If passed, data will not be shown in cells where the mask is True. Cells with missing values are automatically masked.

  • ax : matplotlib Axes, optional The axes in which to draw the plot, otherwise use the currently-active Axes.

  • kwargs :other keyword arguments - All other keyword arguments are passed to matplotlib.axes.Axes.pcolormesh().


  • ax : matplotlib Axes The axes object with the heatmap.


The following code shows how heatmap can be added in Python. You can change different parameters and look at how the output changes.

The following code shows the heatmap for a randomly generated array:
from scipy import stats
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

#add dataset
df = pd.read_csv('dataset.csv')

#plot the graph
ax = sns.heatmap(df)




Fahad Farid
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring