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: IfTrue, 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.pyplotand callingplt.show()to display the plot. - Using incompatible color maps or forgetting to set
annot=Trueif 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():
| Parameter | Description | Example |
|---|---|---|
| data | 2D dataset to plot | pandas DataFrame or 2D list |
| annot | Show data values on cells | True or False |
| cmap | Color map for heatmap colors | 'coolwarm', 'YlGnBu' |
| linewidths | Width of lines between cells | 0.5 |
| linecolor | Color of lines between cells | 'gray' |
| xticklabels | Show or customize x-axis labels | True, False, or list |
| yticklabels | Show or customize y-axis labels | True, 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.