0
0
Data Analysis Pythondata~5 mins

Heatmaps for correlation in Data Analysis Python

Choose your learning style9 modes available
Introduction

A heatmap for correlation helps us see how different things relate to each other using colors. It makes it easy to spot strong or weak connections at a glance.

You want to understand how different features in your data are related.
You need to find which variables move together or oppose each other.
You want a quick visual summary of relationships before deeper analysis.
You are preparing data for machine learning and want to check for redundant features.
Syntax
Data Analysis Python
import seaborn as sns
import matplotlib.pyplot as plt

# Calculate correlation matrix
corr = data.corr()

# Create heatmap
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()

data.corr() calculates the correlation between numeric columns.

sns.heatmap() draws the heatmap; annot=True shows numbers on the map.

Examples
Basic heatmap without numbers or color style.
Data Analysis Python
sns.heatmap(data.corr())
Heatmap with correlation numbers shown on each cell.
Data Analysis Python
sns.heatmap(data.corr(), annot=True)
Heatmap with numbers and a color gradient from cool (blue) to warm (red).
Data Analysis Python
sns.heatmap(data.corr(), annot=True, cmap='coolwarm')
Sample Program

This program creates a small table of height, weight, age, and score. It calculates how these relate to each other and prints the numbers. Then it shows a heatmap with colors and numbers to visualize these relationships.

Data Analysis Python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = pd.DataFrame({
    'height': [150, 160, 170, 180, 190],
    'weight': [50, 60, 65, 80, 90],
    'age': [20, 25, 30, 35, 40],
    'score': [80, 85, 88, 90, 95]
})

# Calculate correlation
corr = data.corr()

# Print correlation matrix
print(corr)

# Plot heatmap
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()
OutputSuccess
Important Notes

Correlation values range from -1 to 1. Close to 1 means strong positive relation, close to -1 means strong negative relation, and near 0 means no relation.

Heatmaps work best with numeric data only.

You can change colors using different cmap options like 'viridis', 'magma', or 'Blues'.

Summary

Heatmaps show correlation between variables using colors and numbers.

They help quickly find strong or weak relationships in data.

Use data.corr() to get correlation and sns.heatmap() to plot.