Heatmap

Annotated Correlation Matrix

Feature correlation heatmap with coefficient values

Output
Annotated Correlation Matrix
Python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)

# Create correlated features
n = 100
features = {
    'Revenue': np.random.randn(n),
    'Marketing': np.random.randn(n),
    'Customers': np.random.randn(n),
    'Satisfaction': np.random.randn(n),
    'Retention': np.random.randn(n),
    'Support_Tickets': np.random.randn(n),
    'NPS': np.random.randn(n),
}

# Add correlations
features['Marketing'] = features['Revenue'] * 0.7 + np.random.randn(n) * 0.3
features['Customers'] = features['Revenue'] * 0.8 + np.random.randn(n) * 0.2
features['NPS'] = features['Satisfaction'] * 0.85 + np.random.randn(n) * 0.15
features['Retention'] = features['Satisfaction'] * 0.6 + np.random.randn(n) * 0.4

df = pd.DataFrame(features)
corr = df.corr()

# Custom diverging NEON colormap
neon_div = LinearSegmentedColormap.from_list('neon_div', ['#27D3F5', '#ffffff', '#F5276C'])

fig, ax = plt.subplots(figsize=(9, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

mask = np.triu(np.ones_like(corr, dtype=bool), k=1)
sns.heatmap(corr, mask=mask, annot=True, fmt='.2f', cmap=neon_div,
            center=0, vmin=-1, vmax=1, square=True, linewidths=2, linecolor='#ffffff',
            annot_kws={'size': 10, 'color': '#1f2937', 'fontweight': '500'},
            cbar_kws={'shrink': 0.8, 'label': 'Correlation'}, ax=ax)

ax.set_title('Feature Correlation Matrix', color='#1f2937', fontsize=14, fontweight='bold', pad=15)
ax.tick_params(colors='#374151', labelsize=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Heatmaps & Density

Did this help you?

Support PyLucid to keep it free & growing

Support