Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Master Data Science with Python

Lesson 39 of 60 · python

Heatmaps and Pairplots for Correlation Analysis

Duration: 20 minutes

Correlation Visualizations

Heatmaps and pairplots help you discover relationships among many variables.

Correlation matrix

corr = df.corr()
print(corr)

Heatmap with seaborn

sns.heatmap(corr, annot=True, fmt='.2f', cmap='coolwarm', linewidths=0.5)
plt.title('Correlation Heatmap')
plt.show()

Pairplot (scatter matrix)

sns.pairplot(df, diag_kind='kde', hue='target')
plt.show()

Annotating the heatmap with significance stars (optional)

import scipy.stats as stats
p_vals = df.corr(method=lambda x, y: stats.pearsonr(x, y)[1])
mask = np.triu(np.ones_like(corr, dtype=bool))
sns.heatmap(corr, mask=mask, cmap='viridis', annot=True, fmt='.2f')
plt.show()

Tips

  • Use mask to hide the upper triangle (redundant).
  • For large datasets, consider sns.clustermap to cluster similar variables.

Info

A strong correlation (|r| > 0.8) may indicate multicollinearity, which can harm some ML models.

Previous: Seaborn Regression PlotsNext: Interactive Visualizations with Plotly