🔥 Heatmap
Step 1: Set index to country names
df = df.set_index('Countries')Step 2: Initial heatmap (includes all columns, including life expectancy)
sns.heatmap(df)Step 3: Drop Life expectancy column for clearer numerical focus
rates = df.drop('Life expectancy', axis=1)Step 4: Adjust visual details
sns.heatmap(rates) # Default
sns.heatmap(rates, linewidth=0.5) # Add cell borders
sns.heatmap(rates, linewidth=0.5, annot=True) # Show numbersStep 5: Customize with color palettes
sns.heatmap(rates, linewidth=0.5, annot=True, cmap='viridis')Step 6: Center colorbar on a specific value
sns.heatmap(rates, linewidth=0.5, annot=True, cmap='viridis', center=40) # Center at 40
sns.heatmap(rates, linewidth=0.5, annot=True, cmap='viridis', center=1) # Center at 1🔍
centerhelps highlight deviation from a key value (e.g., population growth rate = 1) but don’t recommend using center method, default is fine
🧬 Clustermap
Use clustermap to group similar rows/columns using hierarchical clustering.
sns.clustermap(rates)Turn off column clustering (for fixed column order):
sns.clustermap(rates, col_cluster=False)Adjust figure size and colorbar position:
sns.clustermap(
rates,
col_cluster=False,
figsize=(12, 8),
cbar_pos=(-0.1, .2, .03, .4) # [left, bottom, width, height]
)Optional: Clean up row index name
rates.index.set_names('', inplace=True)✅ Key Takeaways
-
heatmapis good for visualizing raw matrix data. -
clustermapis ideal for discovering patterns/groupings. -
Use
annot=Trueto display actual values. -
Use
centerto customize midpoint in colormap (useful for diverging palettes). -
Always clean/preprocess your DataFrame before plotting.