{"spec_id":"choropleth-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nchoropleth-basic: Choropleth Map with Regional Coloring\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 82/100 | Updated: 2026-05-15\n\"\"\"\n\nimport matplotlib.patches as mpatches\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\n\n\n# Data: US states tile grid map with economic data (GDP growth rate %)\n# Tile grid maps are a recognized cartogram technique that ensures equal visual weight per region\nnp.random.seed(42)\n\n# State data with grid positions (row, col) approximating US map layout\n# Values represent GDP growth rate (%) - None indicates missing data\nstates_data = {\n    # Row 0 (top - Pacific Northwest, Northern states)\n    \"WA\": (0, 1, 3.2),\n    \"MT\": (0, 3, 1.8),\n    \"ND\": (0, 5, 2.1),\n    \"MN\": (0, 6, 2.9),\n    \"WI\": (0, 7, 2.3),\n    \"MI\": (0, 8, 1.9),\n    \"NY\": (0, 10, 3.5),\n    \"VT\": (0, 11, 1.5),\n    \"ME\": (0, 12, 1.2),\n    # Row 1\n    \"OR\": (1, 1, 2.8),\n    \"ID\": (1, 2, 3.1),\n    \"WY\": (1, 3, 0.9),\n    \"SD\": (1, 5, 1.7),\n    \"IA\": (1, 6, 2.0),\n    \"IL\": (1, 7, 2.5),\n    \"IN\": (1, 8, 2.2),\n    \"OH\": (1, 9, 1.8),\n    \"PA\": (1, 10, 2.1),\n    \"MA\": (1, 11, 3.8),\n    \"NH\": (1, 12, 2.4),\n    # Row 2\n    \"NV\": (2, 1, 4.1),\n    \"UT\": (2, 2, 4.5),\n    \"CO\": (2, 3, 3.9),\n    \"NE\": (2, 5, 1.6),\n    \"KS\": (2, 6, 1.4),\n    \"MO\": (2, 7, 1.9),\n    \"KY\": (2, 8, 2.0),\n    \"WV\": (2, 9, 0.5),\n    \"VA\": (2, 10, 3.2),\n    \"MD\": (2, 11, 2.8),\n    \"NJ\": (2, 12, 2.6),\n    # Row 3\n    \"CA\": (3, 1, 3.7),\n    \"AZ\": (3, 2, 4.2),\n    \"NM\": (3, 3, 1.3),\n    \"OK\": (3, 5, 1.1),\n    \"AR\": (3, 6, 1.5),\n    \"TN\": (3, 7, 3.0),\n    \"NC\": (3, 9, 3.4),\n    \"SC\": (3, 10, 2.7),\n    \"DE\": (3, 11, 2.3),\n    \"CT\": (3, 12, 2.9),\n    # Row 4 (bottom - Southern states)\n    \"TX\": (4, 3, 3.6),\n    \"LA\": (4, 5, 0.8),\n    \"MS\": (4, 6, 0.6),\n    \"AL\": (4, 7, 1.7),\n    \"GA\": (4, 8, 3.3),\n    \"FL\": (4, 10, 3.8),\n    \"RI\": (4, 12, 2.0),\n    # Missing data examples (show as gray/hatched pattern per spec)\n    \"PR\": (4, 13, None),  # Puerto Rico - no data\n    # Alaska and Hawaii (offset)\n    \"AK\": (5, 0, 0.4),\n    \"HI\": (5, 2, 2.5),\n    \"DC\": (3, 13, None),  # DC - no data available\n}\n\n# Create DataFrame\nrows = []\nfor state, (r, c, val) in states_data.items():\n    rows.append({\"state\": state, \"row\": r, \"col\": c, \"gdp_growth\": val})\ndf = pd.DataFrame(rows)\n\n# Create grid matrix for heatmap (6 rows x 14 cols)\nn_rows, n_cols = 6, 14\ngrid = np.full((n_rows, n_cols), np.nan)\nstate_labels = np.full((n_rows, n_cols), \"\", dtype=object)\nmissing_mask = np.zeros((n_rows, n_cols), dtype=bool)\n\nfor _, row in df.iterrows():\n    r, c = int(row[\"row\"]), int(row[\"col\"])\n    if row[\"gdp_growth\"] is not None and not pd.isna(row[\"gdp_growth\"]):\n        grid[r, c] = row[\"gdp_growth\"]\n    else:\n        missing_mask[r, c] = True  # Mark as missing data\n    state_labels[r, c] = row[\"state\"]\n\n# Set up seaborn styling\nsns.set_theme(style=\"white\", context=\"talk\", font_scale=1.2)\n\n# Create figure with appropriate size for 4800x2700 output\nfig, ax = plt.subplots(figsize=(16, 9))\n\n# Create heatmap using seaborn with masked values for empty cells\nempty_mask = np.isnan(grid) & ~missing_mask  # True for truly empty cells (no state)\nheatmap = sns.heatmap(\n    grid,\n    mask=empty_mask,\n    cmap=\"YlGnBu\",\n    annot=False,  # We'll add custom annotations\n    cbar=True,\n    cbar_kws={\"label\": \"GDP Growth Rate (%)\", \"shrink\": 0.7, \"aspect\": 20, \"pad\": 0.02},\n    linewidths=1.5,  # Reduced from 3 to be less overwhelming\n    linecolor=\"white\",\n    square=True,\n    vmin=0,\n    vmax=5,\n    ax=ax,\n)\n\n# Add gray cells for missing data with hatching pattern\nfor i in range(n_rows):\n    for j in range(n_cols):\n        if missing_mask[i, j]:\n            # Draw gray rectangle with hatching for missing data\n            rect = mpatches.Rectangle(\n                (j, i), 1, 1, fill=True, facecolor=\"#d0d0d0\", edgecolor=\"white\", linewidth=1.5, hatch=\"///\", zorder=2\n            )\n            ax.add_patch(rect)\n\n# Customize colorbar\ncbar = heatmap.collections[0].colorbar\ncbar.ax.tick_params(labelsize=18)\ncbar.set_label(\"GDP Growth Rate (%)\", fontsize=20, labelpad=10)\n\n# Add state code and value annotations\nfor i in range(n_rows):\n    for j in range(n_cols):\n        if state_labels[i, j]:  # If there's a state here\n            # State code (larger, bold)\n            ax.text(\n                j + 0.5,\n                i + 0.35,\n                state_labels[i, j],\n                ha=\"center\",\n                va=\"center\",\n                fontsize=20,\n                fontweight=\"bold\",\n                color=\"#1a1a1a\" if not missing_mask[i, j] else \"#666666\",\n            )\n            # Value or \"N/A\" for missing data (increased from 13pt to 16pt)\n            if not missing_mask[i, j] and not np.isnan(grid[i, j]):\n                ax.text(\n                    j + 0.5,\n                    i + 0.7,\n                    f\"{grid[i, j]:.1f}%\",\n                    ha=\"center\",\n                    va=\"center\",\n                    fontsize=16,\n                    color=\"#333333\",\n                    fontweight=\"medium\",\n                )\n            else:\n                ax.text(\n                    j + 0.5, i + 0.7, \"N/A\", ha=\"center\", va=\"center\", fontsize=16, color=\"#888888\", fontstyle=\"italic\"\n                )\n\n# Styling\nax.set_title(\"choropleth-basic · seaborn · pyplots.ai\", fontsize=26, pad=20, fontweight=\"bold\")\n\n# Remove axis labels and ticks (tile grid doesn't need them)\nax.set_xticks([])\nax.set_yticks([])\nax.set_xlabel(\"\")\nax.set_ylabel(\"\")\n\n# Add legend for missing data\nmissing_patch = mpatches.Patch(facecolor=\"#d0d0d0\", edgecolor=\"white\", hatch=\"///\", label=\"No Data Available\")\nax.legend(handles=[missing_patch], loc=\"lower left\", fontsize=16, framealpha=0.9)\n\n# Add subtitle explaining the visualization\nax.text(\n    0.5,\n    -0.06,\n    \"US States GDP Growth Rate (%) — Tile Grid Choropleth Map\",\n    ha=\"center\",\n    va=\"top\",\n    fontsize=18,\n    color=\"#555555\",\n    transform=ax.transAxes,\n)\n\nplt.tight_layout()\nplt.savefig(\"plot.png\", dpi=300, bbox_inches=\"tight\")\n"}