0
0
Data-analysis-pythonHow-ToBeginner ยท 3 min read

How to Create Heatmap with Seaborn in Python

To create a heatmap in Python using seaborn, use the seaborn.heatmap() function with a 2D dataset like a matrix or dataframe. You can customize colors, labels, and annotations easily by passing parameters to this function.
๐Ÿ“

Syntax

The basic syntax for creating a heatmap with Seaborn is:

  • seaborn.heatmap(data, annot=False, cmap=None, linewidths=0, linecolor='white')

Here:

  • data: 2D data (like a list of lists, NumPy array, or pandas DataFrame) to visualize.
  • annot: If True, shows the data values on the heatmap cells.
  • cmap: Color map to use for colors (e.g., 'coolwarm', 'viridis').
  • linewidths: Width of lines between cells.
  • linecolor: Color of lines between cells.
python
import seaborn as sns
sns.heatmap(data, annot=True, cmap='coolwarm', linewidths=0.5, linecolor='gray')
๐Ÿ’ป

Example

This example shows how to create a heatmap from a small pandas DataFrame with annotations and a color map.

python
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
data = pd.DataFrame({
    'Math': [90, 80, 70],
    'Science': [85, 95, 75],
    'English': [88, 78, 92]
}, index=['Alice', 'Bob', 'Charlie'])

# Create heatmap
sns.heatmap(data, annot=True, cmap='YlGnBu', linewidths=0.5, linecolor='gray')
plt.title('Student Scores Heatmap')
plt.show()
Output
A window opens showing a heatmap with rows labeled Alice, Bob, Charlie and columns Math, Science, English. Each cell shows the score with colors from yellow to blue.
โš ๏ธ

Common Pitfalls

Common mistakes when creating heatmaps with Seaborn include:

  • Passing 1D data instead of 2D data causes errors or unexpected results.
  • Not importing matplotlib.pyplot and calling plt.show() to display the plot.
  • Using incompatible color maps or forgetting to set annot=True if you want to see values.
  • Ignoring axis labels which can make the heatmap hard to understand.
python
import seaborn as sns
import matplotlib.pyplot as plt

# Wrong: 1D data
# sns.heatmap([1, 2, 3, 4])  # This will raise an error

# Right: 2D data
sns.heatmap([[1, 2], [3, 4]], annot=True)
plt.show()
Output
A heatmap with 2 rows and 2 columns showing numbers 1, 2, 3, 4 in cells with colors.
๐Ÿ“Š

Quick Reference

Here is a quick summary of key parameters for seaborn.heatmap():

ParameterDescriptionExample
data2D dataset to plotpandas DataFrame or 2D list
annotShow data values on cellsTrue or False
cmapColor map for heatmap colors'coolwarm', 'YlGnBu'
linewidthsWidth of lines between cells0.5
linecolorColor of lines between cells'gray'
xticklabelsShow or customize x-axis labelsTrue, False, or list
yticklabelsShow or customize y-axis labelsTrue, False, or list
โœ…

Key Takeaways

Use seaborn.heatmap() with 2D data like a DataFrame to create heatmaps easily.
Set annot=True to display values inside heatmap cells for clarity.
Choose a color map (cmap) that fits your data visualization needs.
Always call plt.show() to display the heatmap plot.
Avoid passing 1D data; heatmaps require 2D data structures.