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.
Heatmaps for correlation in 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.
sns.heatmap(data.corr())
sns.heatmap(data.corr(), annot=True)sns.heatmap(data.corr(), annot=True, cmap='coolwarm')
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.
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()
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'.
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.