|
1 | | -""" pyplots.ai |
| 1 | +"""pyplots.ai |
2 | 2 | heatmap-basic: Basic Heatmap |
3 | | -Library: matplotlib 3.10.8 | Python 3.13.11 |
4 | | -Quality: 94/100 | Created: 2025-12-23 |
| 3 | +Library: matplotlib 3.10.8 | Python 3.14.3 |
| 4 | +Quality: /100 | Updated: 2026-02-15 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import matplotlib.pyplot as plt |
8 | 8 | import numpy as np |
| 9 | +from matplotlib.colors import TwoSlopeNorm |
9 | 10 |
|
10 | 11 |
|
11 | | -# Data - correlation-like matrix with meaningful labels |
12 | | -np.random.seed(42) |
13 | | -categories = ["Sales", "Marketing", "Support", "Dev", "HR", "Finance", "Ops", "Legal"] |
14 | | -n = len(categories) |
| 12 | +# Data - department correlation matrix with realistic patterns |
| 13 | +departments = ["Sales", "Marketing", "Support", "Dev", "HR", "Finance", "Ops", "Legal"] |
| 14 | +n = len(departments) |
15 | 15 |
|
16 | | -# Generate a correlation-like symmetric matrix |
17 | | -raw = np.random.randn(n, n) |
18 | | -data = (raw + raw.T) / 2 # Make symmetric |
19 | | -np.fill_diagonal(data, 1) # Perfect correlation on diagonal |
20 | | -data = np.clip(data, -1, 1) # Clip to valid correlation range |
| 16 | +# Craft realistic correlations: related departments correlate more strongly |
| 17 | +data = np.array( |
| 18 | + [ |
| 19 | + [1.00, 0.82, 0.35, 0.12, -0.05, 0.61, 0.44, 0.08], # Sales |
| 20 | + [0.82, 1.00, 0.28, 0.18, 0.10, 0.48, 0.30, 0.05], # Marketing |
| 21 | + [0.35, 0.28, 1.00, 0.55, 0.22, -0.10, 0.60, 0.15], # Support |
| 22 | + [0.12, 0.18, 0.55, 1.00, 0.08, -0.25, 0.42, -0.12], # Dev |
| 23 | + [-0.05, 0.10, 0.22, 0.08, 1.00, 0.30, 0.18, 0.52], # HR |
| 24 | + [0.61, 0.48, -0.10, -0.25, 0.30, 1.00, 0.35, 0.40], # Finance |
| 25 | + [0.44, 0.30, 0.60, 0.42, 0.18, 0.35, 1.00, 0.20], # Ops |
| 26 | + [0.08, 0.05, 0.15, -0.12, 0.52, 0.40, 0.20, 1.00], # Legal |
| 27 | + ] |
| 28 | +) |
21 | 29 |
|
22 | | -# Create plot (3600x3600 px - square format best for heatmaps) |
| 30 | +# Plot (square format for matrix visualization) |
23 | 31 | fig, ax = plt.subplots(figsize=(12, 12)) |
24 | 32 |
|
25 | | -# Heatmap with diverging colormap for positive/negative values |
26 | | -im = ax.imshow(data, cmap="RdBu_r", vmin=-1, vmax=1, aspect="equal") |
| 33 | +# Heatmap with diverging colormap centered at zero |
| 34 | +norm = TwoSlopeNorm(vmin=-1, vcenter=0, vmax=1) |
| 35 | +im = ax.matshow(data, cmap="RdBu_r", norm=norm) |
27 | 36 |
|
28 | | -# Add colorbar |
29 | | -cbar = fig.colorbar(im, ax=ax, shrink=0.8, pad=0.02) |
| 37 | +# Colorbar |
| 38 | +cbar = fig.colorbar(im, ax=ax, shrink=0.78, pad=0.02, aspect=30) |
30 | 39 | cbar.ax.tick_params(labelsize=16) |
31 | | -cbar.set_label("Correlation Coefficient", fontsize=18) |
| 40 | +cbar.set_label("Correlation Coefficient", fontsize=18, labelpad=12) |
| 41 | +cbar.outline.set_visible(False) |
32 | 42 |
|
33 | | -# Set ticks and labels |
| 43 | +# Tick labels |
34 | 44 | ax.set_xticks(np.arange(n)) |
35 | 45 | ax.set_yticks(np.arange(n)) |
36 | | -ax.set_xticklabels(categories, fontsize=16, rotation=45, ha="right") |
37 | | -ax.set_yticklabels(categories, fontsize=16) |
| 46 | +ax.set_xticklabels(departments, fontsize=16, rotation=45, ha="left") |
| 47 | +ax.set_yticklabels(departments, fontsize=16) |
| 48 | +ax.xaxis.set_ticks_position("bottom") |
| 49 | +ax.tick_params(axis="both", length=0) |
38 | 50 |
|
39 | | -# Add value annotations in cells |
| 51 | +# Cell value annotations with adaptive text color |
40 | 52 | for i in range(n): |
41 | 53 | for j in range(n): |
42 | 54 | value = data[i, j] |
43 | | - # Use white text on dark cells, black on light cells |
44 | | - text_color = "white" if abs(value) > 0.5 else "black" |
| 55 | + text_color = "white" if abs(value) > 0.55 else "black" |
45 | 56 | ax.text(j, i, f"{value:.2f}", ha="center", va="center", fontsize=14, color=text_color, fontweight="bold") |
46 | 57 |
|
| 58 | +# Subtle cell borders |
| 59 | +for i in range(n + 1): |
| 60 | + ax.axhline(i - 0.5, color="white", linewidth=1.5) |
| 61 | + ax.axvline(i - 0.5, color="white", linewidth=1.5) |
| 62 | + |
47 | 63 | # Labels and title |
48 | | -ax.set_xlabel("Department", fontsize=20) |
49 | | -ax.set_ylabel("Department", fontsize=20) |
50 | | -ax.set_title("heatmap-basic · matplotlib · pyplots.ai", fontsize=24, pad=15) |
| 64 | +ax.set_xlabel("Department", fontsize=20, labelpad=12) |
| 65 | +ax.set_ylabel("Department", fontsize=20, labelpad=12) |
| 66 | +ax.set_title( |
| 67 | + "Department Correlation · heatmap-basic · matplotlib · pyplots.ai", fontsize=24, fontweight="medium", pad=15 |
| 68 | +) |
51 | 69 |
|
52 | 70 | plt.tight_layout() |
53 | 71 | plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
0 commit comments