{"spec_id":"hexbin-map-geographic","library":"makie","language":"julia","code":"# anyplot.ai\n# hexbin-map-geographic: Hexagonal Binning Map\n# Library: makie 0.24.16 | Julia 1.13.4\n# Quality: 84/100 | Updated: 2026-11-27\n\nusing CairoMakie\nusing Colors\nusing DataFrames\nusing Random\n\n# Theme tokens\nconst THEME       = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG     = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst INK         = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT    = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nconst GRID_COLOR  = THEME == \"light\" ?\n    RGBAf(26/255f0, 26/255f0, 23/255f0, 0.12f0) :\n    RGBAf(240/255f0, 239/255f0, 232/255f0, 0.12f0)\n\n# Water fill and coastline colors for the geographic base map\nconst WATER_FILL  = THEME == \"light\" ?\n    RGBAf(0.58f0, 0.75f0, 0.88f0, 0.60f0) :\n    RGBAf(0.18f0, 0.35f0, 0.56f0, 0.70f0)\nconst COAST_LINE  = THEME == \"light\" ?\n    RGBAf(0.20f0, 0.38f0, 0.55f0, 0.95f0) :\n    RGBAf(0.50f0, 0.70f0, 0.88f0, 0.90f0)\n\n# Imprint sequential ramp for density (fields: colored cells fill the map)\nconst ANYPLOT_SEQ_FIELDS = THEME == \"light\" ?\n    cgrad([colorant\"#66E2B2\", colorant\"#009E73\", colorant\"#024582\", colorant\"#351E45\"], [0.0, 0.369, 0.803, 1.0]) :\n    cgrad([colorant\"#404C02\", colorant\"#009E73\", colorant\"#CDD9FF\"], [0.0, 0.487, 1.0])\n\n# Columns\nconst LON = \"Longitude\"  # role: x\nconst LAT = \"Latitude\"   # role: y\n\n# Map extent\nconst LON_LO, LON_HI = -122.515, -122.360\nconst LAT_LO, LAT_HI =   37.700,   37.835\n\n# Simplified coastlines (Makie auto-closes last -> first).\n# SF Bay water: the eastern SF waterfront (south -> north), then the box corners.\nbay_pts = Point2f[\n    (-122.374, 37.700), (-122.376, 37.748), (-122.381, 37.762),\n    (-122.386, 37.776), (-122.393, 37.790), (-122.400, 37.800),\n    (-122.407, 37.805), (-122.414, 37.808), (-122.432, 37.804),\n    (-122.443, 37.803), (-122.455, 37.803), (-122.464, 37.804),\n    (-122.479, 37.808),                       # Golden Gate Bridge, SF side\n    (-122.479, LAT_HI), (LON_HI, LAT_HI), (LON_HI, LAT_LO),\n]\n\n# Pacific Ocean: the western SF coast (north -> south), then the box corners.\npac_pts = Point2f[\n    (-122.479, 37.808),                       # Golden Gate Bridge, SF side\n    (-122.484, 37.800), (-122.493, 37.798),\n    (-122.499, 37.794), (-122.505, 37.789),\n    (-122.509, 37.782), (-122.511, 37.770),\n    (-122.512, 37.748), (-122.511, 37.710),\n    (LON_LO, LAT_LO), (LON_LO, LAT_HI),\n    (-122.479, LAT_HI),\n]\n\n# Ray-casting point-in-polygon test, used to keep ride starts on land\nfunction in_polygon(x, y, poly)\n    inside = false\n    j = length(poly)\n    for i in eachindex(poly)\n        xi, yi = poly[i]\n        xj, yj = poly[j]\n        if (yi > y) != (yj > y) && x < (xj - xi) * (y - yi) / (yj - yi) + xi\n            inside = !inside\n        end\n        j = i\n    end\n    return inside\nend\n\n# Data: synthetic bike-share ride starts in San Francisco, land only\nRandom.seed!(42)\ncluster_centers = [\n    (-122.4090, 37.7830),  # Union Square / Downtown\n    (-122.4194, 37.7599),  # Mission District\n    (-122.4030, 37.7700),  # SoMa\n    (-122.4352, 37.7940),  # Marina\n    (-122.4447, 37.7698),  # Haight-Ashbury\n]\ncluster_weights = [0.33, 0.22, 0.20, 0.13, 0.12]\ncluster_stds    = [0.011, 0.012, 0.009, 0.008, 0.013]\n\nn_total = 14000\nlons = Float64[]\nlats = Float64[]\nfor ((cx, cy), w, σ) in zip(cluster_centers, cluster_weights, cluster_stds)\n    n_i = round(Int, n_total * w)\n    append!(lons, cx .+ σ .* randn(n_i))\n    append!(lats, cy .+ σ .* randn(n_i))\nend\n\non_land = [\n    LON_LO <= x <= LON_HI && LAT_LO <= y <= LAT_HI &&\n    !in_polygon(x, y, bay_pts) && !in_polygon(x, y, pac_pts)\n    for (x, y) in zip(lons, lats)\n]\ndf = DataFrame(LON => lons[on_land], LAT => lats[on_land])\n\n# Plot\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title              = \"SF Bike-Share · hexbin-map-geographic · julia · makie · anyplot.ai\",\n    titlesize          = 20,\n    titlecolor         = INK,\n    xlabel             = LON,\n    ylabel             = LAT,\n    xlabelsize         = 14,\n    ylabelsize         = 14,\n    xticklabelsize     = 12,\n    yticklabelsize     = 12,\n    xlabelcolor        = INK,\n    ylabelcolor        = INK,\n    xticklabelcolor    = INK_SOFT,\n    yticklabelcolor    = INK_SOFT,\n    xtickcolor         = INK_SOFT,\n    ytickcolor         = INK_SOFT,\n    backgroundcolor    = PAGE_BG,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinecolor     = INK_SOFT,\n    bottomspinecolor   = INK_SOFT,\n    xgridcolor         = GRID_COLOR,\n    ygridcolor         = GRID_COLOR,\n    xminorgridvisible  = false,\n    yminorgridvisible  = false,\n)\n\n# Base map: water bodies drawn before the hexbins so the shoreline shows through\npoly!(ax, bay_pts; color = WATER_FILL, strokecolor = COAST_LINE, strokewidth = 1.2)\npoly!(ax, pac_pts; color = WATER_FILL, strokecolor = COAST_LINE, strokewidth = 1.2)\n\n# Hexagonal density map overlaid on the base map\nhb = hexbin!(ax, df[!, LON], df[!, LAT];\n    cellsize    = 0.009,\n    colormap    = ANYPLOT_SEQ_FIELDS,\n    alpha       = 0.82,\n    threshold   = 1,\n    strokecolor = PAGE_BG,\n    strokewidth = 0.5,\n)\n\nColorbar(fig[1, 2], hb;\n    label           = \"Ride Count\",\n    labelsize       = 13,\n    labelcolor      = INK,\n    ticklabelsize   = 11,\n    ticklabelcolor  = INK_SOFT,\n    tickcolor       = INK_SOFT,\n    width           = 18,\n)\n\nlimits!(ax, LON_LO, LON_HI, LAT_LO, LAT_HI)\ncolgap!(fig.layout, 12)\n\n# Save\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}