{"spec_id":"hexbin-map-geographic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nhexbin-map-geographic: Hexagonal Binning Map\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-27\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import LinearSegmentedColormap\nfrom matplotlib.patches import Rectangle\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# Continuous colormap — imprint_seq (green → blue, single-polarity)\nimprint_seq = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#4467A3\"])\n\n# Base map surface colors\nWATER_BG = \"#C8DFF0\" if THEME == \"light\" else \"#1C2D3A\"\nLAND_BG = \"#E8E5DB\" if THEME == \"light\" else \"#282820\"\n\n# Data: NYC taxi pickup locations (5,000 simulated points)\nnp.random.seed(42)\nn_points = 5000\n\nlat_manhattan = np.random.normal(40.758, 0.030, 2500)\nlon_manhattan = np.random.normal(-73.985, 0.015, 2500)\nlat_brooklyn = np.random.normal(40.680, 0.040, 1500)\nlon_brooklyn = np.random.normal(-73.960, 0.030, 1500)\nlat_queens = np.random.normal(40.730, 0.030, 1000)\nlon_queens = np.random.normal(-73.850, 0.040, 1000)\n\nlat = np.concatenate([lat_manhattan, lat_brooklyn, lat_queens])\nlon = np.concatenate([lon_manhattan, lon_brooklyn, lon_queens])\nvalues = np.random.exponential(15, n_points) + 5\n\n# Plot — landscape 3200 × 1800 px\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\n\n# Base map: water background + land area rectangle\nax.set_facecolor(WATER_BG)\nax.add_patch(Rectangle((-74.05, 40.58), 0.35, 0.32, linewidth=0, facecolor=LAND_BG, zorder=0))\n\n# Hexbin: mean trip fare aggregated per hex cell\nhb = ax.hexbin(\n    lon,\n    lat,\n    C=values,\n    gridsize=35,\n    reduce_C_function=np.mean,\n    cmap=imprint_seq,\n    alpha=0.85,\n    edgecolors=INK_MUTED,\n    linewidths=0.3,\n    mincnt=1,\n    zorder=2,\n)\n\n# Colorbar\ncbar = fig.colorbar(hb, ax=ax, shrink=0.90, pad=0.02, aspect=28, fraction=0.030)\ncbar.set_label(\"Mean Trip Fare ($)\", fontsize=9, color=INK, labelpad=8)\ncbar.ax.tick_params(labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\n\n# Axis limits and coordinate formatters\nax.set_xlim(-74.05, -73.70)\nax.set_ylim(40.58, 40.90)\nax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: f\"{abs(x):.2f}°W\"))\nax.yaxis.set_major_formatter(plt.FuncFormatter(lambda y, p: f\"{y:.2f}°N\"))\n\n# Style\nax.set_xlabel(\"Longitude\", fontsize=10, color=INK)\nax.set_ylabel(\"Latitude\", fontsize=10, color=INK)\n\ntitle = \"hexbin-map-geographic · python · matplotlib · anyplot.ai\"\nn = len(title)\ntitle_fs = max(8, round(12 * 67 / n)) if n > 67 else 12\nax.set_title(title, fontsize=title_fs, fontweight=\"medium\", color=INK, pad=10)\n\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\nax.grid(True, alpha=0.12, linestyle=\"--\", color=INK, zorder=1, linewidth=0.5)\nfor spine in ax.spines.values():\n    spine.set_color(INK_SOFT)\n    spine.set_linewidth(0.5)\n\n# Borough annotations\nfor x, y, name in [(-73.985, 40.758, \"Manhattan\"), (-73.960, 40.680, \"Brooklyn\"), (-73.850, 40.730, \"Queens\")]:\n    ax.annotate(\n        name,\n        xy=(x, y),\n        xytext=(x + 0.020, y + 0.025),\n        fontsize=9,\n        fontweight=\"bold\",\n        color=INK,\n        ha=\"left\",\n        bbox={\"boxstyle\": \"round,pad=0.3\", \"facecolor\": ELEVATED_BG, \"alpha\": 0.88, \"edgecolor\": INK_SOFT, \"linewidth\": 0.5},\n        zorder=5,\n    )\n\n# Layout — subplots_adjust controls padding; no bbox_inches='tight' in savefig\nfig.subplots_adjust(left=0.10, right=0.92, top=0.92, bottom=0.12)\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}