{"spec_id":"heatmap-correlation","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nheatmap-correlation: Correlation Matrix Heatmap\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 76/100 | Updated: 2026-05-08\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\n\n\n# Theme tokens\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\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\n\n# Data - Real estate features for correlation analysis\nnp.random.seed(42)\nn_samples = 200\n\n# Generate realistic correlated real estate data\nsqft = np.random.normal(2000, 500, n_samples)\nbedrooms = np.round(sqft / 600 + np.random.normal(0, 0.5, n_samples)).clip(1, 6)\nbathrooms = np.round(bedrooms * 0.7 + np.random.normal(0, 0.3, n_samples)).clip(1, 4)\nage = np.random.exponential(20, n_samples).clip(0, 80)\nprice = sqft * 150 + bedrooms * 15000 - age * 2000 + np.random.normal(0, 30000, n_samples)\ngarage = np.round(bedrooms * 0.4 + np.random.normal(0, 0.3, n_samples)).clip(0, 3)\nlot_size = sqft * 2 + np.random.normal(0, 1000, n_samples)\ndistance_downtown = np.random.exponential(10, n_samples).clip(1, 40)\ncrime_rate = distance_downtown * 0.3 + np.random.normal(5, 2, n_samples)\n\n# Create DataFrame with descriptive column names including units\ndf = pd.DataFrame(\n    {\n        \"Price ($K)\": price / 1000,\n        \"Area (sq ft)\": sqft,\n        \"Bedrooms\": bedrooms,\n        \"Bathrooms\": bathrooms,\n        \"Age (years)\": age,\n        \"Garage Spots\": garage,\n        \"Lot (sq ft)\": lot_size,\n        \"Distance (mi)\": distance_downtown,\n        \"Crime Index\": crime_rate,\n    }\n)\n\n# Compute correlation matrix\ncorr_matrix = df.corr()\n\n# Create mask for upper triangle\nmask = np.triu(np.ones_like(corr_matrix, dtype=bool), k=1)\n\n# Plot - Square format for symmetric matrix\nfig, ax = plt.subplots(figsize=(12, 12), facecolor=PAGE_BG)\n\n# Create heatmap with seaborn\nsns.heatmap(\n    corr_matrix,\n    mask=mask,\n    annot=True,\n    fmt=\".2f\",\n    cmap=\"BrBG\",\n    center=0,\n    vmin=-1,\n    vmax=1,\n    square=True,\n    linewidths=0.5,\n    linecolor=INK_SOFT,\n    cbar_kws={\"shrink\": 0.8, \"label\": \"Correlation Coefficient\"},\n    annot_kws={\"size\": 14, \"color\": INK},\n    ax=ax,\n)\n\n# Style\nax.set_facecolor(PAGE_BG)\nax.set_title(\"heatmap-correlation · seaborn · anyplot.ai\", fontsize=24, pad=20, color=INK)\nax.set_xlabel(\"\", fontsize=0)\nax.set_ylabel(\"\", fontsize=0)\nax.tick_params(axis=\"both\", labelsize=14, colors=INK_SOFT)\n\n# Rotate labels for readability\nplt.xticks(rotation=45, ha=\"right\")\nplt.yticks(rotation=0)\n\n# Style colorbar\ncbar = ax.collections[0].colorbar\ncbar.ax.tick_params(labelsize=14, colors=INK_SOFT)\ncbar.ax.set_ylabel(\"Correlation Coefficient\", fontsize=16, color=INK)\ncbar.ax.yaxis.set_label_position(\"right\")\ncbar.ax.set_facecolor(PAGE_BG)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}