Page10/13
Heatmaps & 2D Data Visualization Β· Page 1 of 1
Creating Heatmaps
Heatmaps & 2D Data Visualization
What is a Heatmap?
A heatmap uses color intensity to represent values in a 2D matrix. Perfect for correlation matrices, confusion matrices, and time-series grids.
Basic Heatmap with imshow()
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(10, 10)
fig, ax = plt.subplots()
im = ax.imshow(data, cmap='viridis') # colormap
ax.set_title('2D Data Heatmap')
plt.colorbar(im, ax=ax) # add color scale
Heatmaps with Text Labels
Perfect for correlation matrices where you want to see exact values:
import seaborn as sns
# Correlation matrix
corr_matrix = df.corr()
fig, ax = plt.subplots(figsize=(8, 8))
sns.heatmap(corr_matrix, annot=True, fmt='.2f', cmap='coolwarm',
cbar_kws={'label': 'Correlation'}, ax=ax)
ax.set_title('Feature Correlation Matrix')
Popular Colormaps (Color Schemes)
| Colormap | Use Case |
|---|---|
viridis | General-purpose (colorblind-friendly) |
coolwarm | Diverging data (negative β positive) |
RdYlGn | Red-Yellow-Green (good-bad) |
Reds | Sequential (light β dark) |
Tip: Always use colorblind-friendly colormaps. Avoid jet and rainbow!
main.py
Loading...
OUTPUT
βΆClick "Run Code" to executeβ¦