{"spec_id":"heatmap-annotated","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nheatmap-annotated: Annotated Heatmap\nLibrary: matplotlib 3.11.1 | Python 3.13.14\nQuality: 95/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import LinearSegmentedColormap\n\n\n# Theme tokens (see prompts/default-style-guide.md \"Theme-adaptive Chrome\")\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nCELL_LIGHT_TEXT = \"#F0EFE8\"  # fixed light-ink chrome value, for contrast on dark cell fills in either theme\n\n# Imprint diverging colormap — correlations have a meaningful zero midpoint\nmidpoint = PAGE_BG\nimprint_div = LinearSegmentedColormap.from_list(\"imprint_div\", [\"#AE3030\", midpoint, \"#4467A3\"])\n\n# Data: laboratory measurement correlations\nnp.random.seed(42)\nmeasurements = [\"Temperature\", \"pH\", \"Viscosity\", \"Density\", \"Turbidity\", \"Conductivity\", \"Salinity\", \"Pressure\"]\nn = len(measurements)\n\n# Generate a realistic correlation matrix (symmetric, diagonal = 1)\nbase = np.random.randn(n, n) * 0.3\ncorrelation = (base + base.T) / 2\nnp.fill_diagonal(correlation, 1.0)\ncorrelation = np.clip(correlation, -1, 1)\n\n# Add realistic scientific correlations\ncorrelation[0, 1] = correlation[1, 0] = -0.68  # Temperature-pH: negative\ncorrelation[0, 2] = correlation[2, 0] = 0.55  # Temperature-Viscosity: positive\ncorrelation[3, 5] = correlation[5, 3] = 0.77  # Density-Conductivity: strong positive\ncorrelation[4, 5] = correlation[5, 4] = -0.62  # Turbidity-Conductivity: negative\ncorrelation[6, 7] = correlation[7, 6] = 0.81  # Salinity-Pressure: strong positive\ncorrelation[1, 4] = correlation[4, 1] = 0.45  # pH-Turbidity: positive\n\n# Plot — square format for a symmetric matrix (see default-style-guide.md \"Visual Sizing Defaults\")\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nim = ax.imshow(correlation, cmap=imprint_div, vmin=-1, vmax=1, aspect=\"equal\")\n\n# Colorbar\ncbar = ax.figure.colorbar(im, ax=ax, shrink=0.8, aspect=30)\ncbar.ax.tick_params(labelsize=8, colors=INK_SOFT)\ncbar.set_label(\"Correlation Coefficient\", fontsize=10, labelpad=10, color=INK)\ncbar.outline.set_edgecolor(INK_SOFT)\ncbar.outline.set_linewidth(1)\n\n# Ticks and category labels\nax.set_xticks(np.arange(n))\nax.set_yticks(np.arange(n))\nax.set_xticklabels(measurements, fontsize=9, color=INK_SOFT)\nax.set_yticklabels(measurements, fontsize=9, color=INK_SOFT)\nplt.setp(ax.get_xticklabels(), rotation=45, ha=\"right\", rotation_mode=\"anchor\")\n\n# Cell annotations — text color and weight computed from the cell's actual render\n# luminance (not a fixed value threshold), so contrast stays correct across the\n# full imprint_div range; magnitude scales size/weight to reinforce the strongest\n# relationships visually, echoing the color-driven hierarchy.\nfor i in range(n):\n    for j in range(n):\n        value = correlation[i, j]\n        r, g, b, _ = im.cmap(im.norm(value))\n        luminance = 0.299 * r + 0.587 * g + 0.114 * b\n        text_color = INK if luminance > 0.5 else CELL_LIGHT_TEXT\n        weight = \"bold\" if abs(value) >= 0.5 else \"normal\"\n        size = 9 + 3 * abs(value)\n        ax.text(j, i, f\"{value:.2f}\", ha=\"center\", va=\"center\", color=text_color, fontsize=size, fontweight=weight)\n\n# Styling — suptitle centers on the full figure (incl. colorbar), unlike ax.set_title\nfig.suptitle(\"heatmap-annotated · python · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK)\nax.set_xlabel(\"Laboratory Measurements\", fontsize=10, labelpad=10, color=INK)\nax.set_ylabel(\"Laboratory Measurements\", fontsize=10, labelpad=10, color=INK)\n\n# Subtle grid between cells\nax.set_xticks(np.arange(n + 1) - 0.5, minor=True)\nax.set_yticks(np.arange(n + 1) - 0.5, minor=True)\nax.grid(which=\"minor\", color=INK_SOFT, linestyle=\"-\", linewidth=1, alpha=0.3)\nax.tick_params(which=\"minor\", bottom=False, left=False)\n\nplt.tight_layout(rect=(0, 0, 1, 0.96))\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)  # bbox_inches MUST stay default (None)\n"}