{"spec_id":"histogram-2d","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nhistogram-2d: 2D Histogram Heatmap\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-08\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Set theme\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data - Customer behavior: age vs. annual spending (physics experiment context)\n# Using a real-world distribution of customer data with correlation\nnp.random.seed(42)\nn_points = 3000\nage = np.random.normal(45, 15, n_points)\nage = np.clip(age, 18, 75)  # Realistic age range\nspending = 500 + 8 * (age - 18) + np.random.normal(0, 800, n_points)\nspending = np.clip(spending, 0, 6000)  # Realistic spending range\n\n# Create figure with marginal histograms using JointGrid\ng = sns.JointGrid(x=age, y=spending, height=10, ratio=5, space=0.2)\n\n# Plot 2D histogram heatmap with improved bin sizing\nsns.histplot(x=age, y=spending, bins=35, cmap=\"viridis\", cbar=True, cbar_kws={\"label\": \"Customer Count\"}, ax=g.ax_joint)\n\n# Plot marginal 1D histograms with theme-adaptive color\nmarginal_color = \"#009E73\"  # Okabe-Ito brand green\nsns.histplot(x=age, bins=30, color=marginal_color, alpha=0.7, edgecolor=PAGE_BG, linewidth=0.5, ax=g.ax_marg_x)\nsns.histplot(y=spending, bins=30, color=marginal_color, alpha=0.7, edgecolor=PAGE_BG, linewidth=0.5, ax=g.ax_marg_y)\n\n# Styling - scale fonts for large canvas\ng.ax_joint.set_xlabel(\"Customer Age (years)\", fontsize=20, color=INK, labelpad=10)\ng.ax_joint.set_ylabel(\"Annual Spending ($)\", fontsize=20, color=INK, labelpad=10)\ng.ax_joint.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Style colorbar\ncbar = g.ax_joint.collections[0].colorbar\ncbar.ax.tick_params(labelsize=14, colors=INK_SOFT)\ncbar.ax.yaxis.label.set_size(16)\ncbar.ax.yaxis.label.set_color(INK)\n\n# Title\ng.figure.suptitle(\"histogram-2d · seaborn · anyplot.ai\", fontsize=24, y=0.98, color=INK)\n\n# Hide marginal axis labels for cleaner look\ng.ax_marg_x.set_ylabel(\"\")\ng.ax_marg_y.set_xlabel(\"\")\ng.ax_marg_x.tick_params(labelsize=12, colors=INK_SOFT)\ng.ax_marg_y.tick_params(labelsize=12, colors=INK_SOFT)\n\n# Adjust layout for better spacing\ng.figure.set_size_inches(16, 9)\ng.figure.tight_layout()\ng.figure.subplots_adjust(top=0.93)\n\n# Save at 4800x2700\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\nplt.close()\n"}