{"spec_id":"heatmap-geographic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nheatmap-geographic: Geographic Heatmap for Spatial Density\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 82/100 | Updated: 2026-05-19\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Data: Simulated environmental monitoring stations across California\nnp.random.seed(42)\n\nn_points = 500\n\n# Create clusters representing different monitoring regions\ncoast_lat = np.random.normal(36.5, 0.8, n_points // 3)\ncoast_lon = np.random.normal(-121.5, 0.5, n_points // 3)\n\nsocal_lat = np.random.normal(34.0, 0.6, n_points // 3)\nsocal_lon = np.random.normal(-118.0, 0.7, n_points // 3)\n\nnorcal_lat = np.random.normal(38.5, 0.5, n_points // 3 + n_points % 3)\nnorcal_lon = np.random.normal(-122.5, 0.4, n_points // 3 + n_points % 3)\n\nlatitudes = np.concatenate([coast_lat, socal_lat, norcal_lat])\nlongitudes = np.concatenate([coast_lon, socal_lon, norcal_lon])\n\n# AQI values: realistic range 0–150 (Good to Unhealthy)\nvalues = np.clip(np.random.gamma(shape=2.5, scale=28, size=len(latitudes)), 5, 150)\n\n# Define map boundaries for California\nlat_min, lat_max = 32.5, 42.0\nlon_min, lon_max = -125.0, -114.0\n\n# Create 2D histogram for density estimation\ngrid_resolution = 150\nlat_bins = np.linspace(lat_min, lat_max, grid_resolution)\nlon_bins = np.linspace(lon_min, lon_max, grid_resolution)\n\nheatmap, lat_edges, lon_edges = np.histogram2d(\n    latitudes, longitudes, bins=[lat_bins, lon_bins], weights=values, density=False\n)\n\n# Gaussian smoothing for continuous appearance (KDE approximation)\nsigma = 3\nkernel_size = int(6 * sigma + 1)\nif kernel_size % 2 == 0:\n    kernel_size += 1\nkernel_x = np.arange(kernel_size) - kernel_size // 2\nkernel_1d = np.exp(-(kernel_x**2) / (2 * sigma**2))\nkernel_1d = kernel_1d / kernel_1d.sum()\n\nheatmap_smooth = np.apply_along_axis(lambda row: np.convolve(row, kernel_1d, mode=\"same\"), axis=0, arr=heatmap)\nheatmap_smooth = np.apply_along_axis(lambda col: np.convolve(col, kernel_1d, mode=\"same\"), axis=1, arr=heatmap_smooth)\n\n# Mask zero/near-zero values for transparency\nheatmap_masked = np.ma.masked_where(heatmap_smooth < 0.1, heatmap_smooth)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# California outline approximation\ncoast_lons = [\n    -124.4,\n    -124.2,\n    -123.8,\n    -122.4,\n    -122.0,\n    -121.5,\n    -121.0,\n    -120.5,\n    -120.0,\n    -119.5,\n    -119.0,\n    -118.5,\n    -118.0,\n    -117.5,\n    -117.2,\n    -117.0,\n    -117.1,\n    -117.3,\n]\ncoast_lats = [\n    42.0,\n    40.5,\n    39.0,\n    37.8,\n    37.5,\n    36.8,\n    36.5,\n    35.5,\n    35.0,\n    34.5,\n    34.2,\n    34.0,\n    33.8,\n    33.2,\n    33.0,\n    32.7,\n    32.5,\n    32.5,\n]\nax.plot(coast_lons, coast_lats, color=INK_SOFT, linewidth=2, alpha=0.7, label=\"Coastline\")\n\neast_lons = [-117.3, -117.0, -116.5, -115.5, -114.6, -114.6, -120.0, -120.0, -121.0, -122.0, -123.0, -124.2, -124.4]\neast_lats = [32.5, 33.0, 33.5, 34.0, 34.8, 36.0, 39.0, 40.0, 41.0, 41.5, 42.0, 42.0, 42.0]\nax.plot(east_lons, east_lats, color=INK_SOFT, linewidth=2, alpha=0.7)\n\n# Heatmap layer\nextent = [lon_min, lon_max, lat_min, lat_max]\nim = ax.imshow(\n    heatmap_masked.T, extent=extent, origin=\"lower\", aspect=\"auto\", cmap=\"YlOrRd\", alpha=0.78, interpolation=\"bilinear\"\n)\n\n# Sensor locations (slightly more visible than before)\nax.scatter(longitudes, latitudes, s=25, c=\"#4467A3\", alpha=0.45, edgecolors=\"none\", label=\"Sensor Locations\")\n\n# Colorbar\ncbar = plt.colorbar(im, ax=ax, shrink=0.8, pad=0.02)\ncbar.set_label(\"Air Quality Index (weighted density)\", fontsize=18, color=INK_SOFT)\ncbar.ax.tick_params(labelsize=14, colors=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\n\n# Style\nax.set_xlabel(\"Longitude\", fontsize=20, color=INK)\nax.set_ylabel(\"Latitude\", fontsize=20, color=INK)\nax.set_title(\"heatmap-geographic · python · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.set_xlim(lon_min, lon_max)\nax.set_ylim(lat_min, lat_max)\n\nfor spine in ax.spines.values():\n    spine.set_color(INK_SOFT)\n\nax.grid(True, alpha=0.12, linestyle=\"--\", color=INK)\n\n# Legend positioned in lower right to avoid overlap with dense northern clusters\nleg = ax.legend(loc=\"lower right\", fontsize=14)\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nplt.setp(leg.get_texts(), color=INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}