{"spec_id":"heatmap-geographic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nheatmap-geographic: Geographic Heatmap for Spatial Density\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\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\"\nMAP_STYLE = \"carto-positron\" if THEME == \"light\" else \"carto-darkmatter\"\n\n# Data - Activity density around San Francisco Bay Area\nnp.random.seed(42)\n\n# Hotspot 1: Downtown SF\nlat1 = np.random.normal(37.79, 0.02, 400)\nlon1 = np.random.normal(-122.40, 0.02, 400)\nval1 = np.random.uniform(0.6, 1.0, 400)\n\n# Hotspot 2: Oakland\nlat2 = np.random.normal(37.80, 0.025, 350)\nlon2 = np.random.normal(-122.27, 0.025, 350)\nval2 = np.random.uniform(0.5, 0.9, 350)\n\n# Hotspot 3: Berkeley\nlat3 = np.random.normal(37.87, 0.015, 250)\nlon3 = np.random.normal(-122.26, 0.015, 250)\nval3 = np.random.uniform(0.4, 0.8, 250)\n\n# Hotspot 4: South SF\nlat4 = np.random.normal(37.65, 0.03, 300)\nlon4 = np.random.normal(-122.40, 0.03, 300)\nval4 = np.random.uniform(0.3, 0.7, 300)\n\n# Scattered background points\nlat_bg = np.random.uniform(37.5, 38.0, 200)\nlon_bg = np.random.uniform(-122.6, -122.1, 200)\nval_bg = np.random.uniform(0.1, 0.4, 200)\n\n# Combine all data\nlatitudes = np.concatenate([lat1, lat2, lat3, lat4, lat_bg])\nlongitudes = np.concatenate([lon1, lon2, lon3, lon4, lon_bg])\nvalues = np.concatenate([val1, val2, val3, val4, val_bg])\n\n# Plot\nfig = go.Figure()\n\nfig.add_trace(\n    go.Densitymap(\n        lat=latitudes,\n        lon=longitudes,\n        z=values,\n        radius=15,\n        colorscale=\"YlOrRd\",\n        opacity=0.7,\n        showscale=True,\n        colorbar={\n            \"title\": {\"text\": \"Intensity\", \"font\": {\"size\": 20, \"color\": INK}},\n            \"tickfont\": {\"size\": 16, \"color\": INK_SOFT},\n            \"len\": 0.6,\n            \"thickness\": 25,\n            \"x\": 1.02,\n        },\n        hovertemplate=\"Lat: %{lat:.4f}<br>Lon: %{lon:.4f}<br>Value: %{z:.2f}<extra></extra>\",\n    )\n)\n\nfig.update_layout(\n    title={\n        \"text\": \"Activity Density · heatmap-geographic · python · plotly · anyplot.ai\",\n        \"font\": {\"size\": 28, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    map={\"style\": MAP_STYLE, \"center\": {\"lat\": 37.75, \"lon\": -122.35}, \"zoom\": 9.5},\n    paper_bgcolor=PAGE_BG,\n    margin={\"l\": 20, \"r\": 100, \"t\": 80, \"b\": 20},\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}