{"spec_id":"hexbin-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nhexbin-basic: Basic Hexbin Plot\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-29\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport seaborn as sns\nfrom matplotlib.colors import LinearSegmentedColormap, LogNorm\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\"\n\n# Imprint sequential colormap for continuous density data\nimprint_seq = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#4467A3\"])\n\n# Data — NYC GPS coordinates for urban traffic hotspot analysis\nnp.random.seed(42)\nn_points = 50000\n\ndowntown = np.random.multivariate_normal([-73.985, 40.748], [[0.0001, 0.00005], [0.00005, 0.0001]], n_points // 2)\nairport = np.random.multivariate_normal([-73.875, 40.775], [[0.00008, -0.00003], [-0.00003, 0.00008]], n_points // 3)\nshopping = np.random.multivariate_normal([-73.965, 40.785], [[0.00004, 0], [0, 0.00006]], n_points // 6)\n\nlongitude = np.concatenate([downtown[:, 0], airport[:, 0], shopping[:, 0]])\nlatitude = np.concatenate([downtown[:, 1], airport[:, 1], shopping[:, 1]])\n\n# Plot\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Square canvas: height=6 @ dpi=400 → 2400×2400 px\ng = sns.JointGrid(x=longitude, y=latitude, height=6, ratio=5, space=0.15)\ng.figure.set_dpi(400)\ng.figure.patch.set_facecolor(PAGE_BG)\n\n# Main hexbin with Imprint sequential colormap and log normalization\nhb = g.ax_joint.hexbin(longitude, latitude, gridsize=35, cmap=imprint_seq, mincnt=1, norm=LogNorm(), edgecolors=\"none\")\n\n# Marginal KDE distributions — seaborn's distinctive JointGrid feature\ng.plot_marginals(sns.kdeplot, color=\"#009E73\", fill=True, alpha=0.35, linewidth=1.5)\n\n# Remove grid and spines from marginals for polished appearance\nfor ax_marg in [g.ax_marg_x, g.ax_marg_y]:\n    ax_marg.grid(False)\n    ax_marg.set_facecolor(PAGE_BG)\n    for spine in ax_marg.spines.values():\n        spine.set_visible(False)\n\n# Style — joint axes\ng.ax_joint.set_xlabel(\"Longitude (°W)\", fontsize=10, color=INK)\ng.ax_joint.set_ylabel(\"Latitude (°N)\", fontsize=10, color=INK)\ng.ax_joint.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\ng.ax_joint.grid(True, alpha=0.15, linewidth=0.8, color=INK)\ng.ax_joint.spines[\"top\"].set_visible(False)\ng.ax_joint.spines[\"right\"].set_visible(False)\nfor spine_name in [\"left\", \"bottom\"]:\n    g.ax_joint.spines[spine_name].set_color(INK_SOFT)\n\n# Reserve top for title and right for colorbar without crowding the marginal\ng.figure.subplots_adjust(top=0.93, right=0.83, left=0.1, bottom=0.09, hspace=0.15, wspace=0.15)\n\n# Colorbar in dedicated right-side space — positioned to avoid overlapping marginal\ncbar_ax = g.figure.add_axes([0.86, 0.1, 0.025, 0.56])\ncbar = g.figure.colorbar(hb, cax=cbar_ax)\ncbar.set_label(\"Point Count (log scale)\", fontsize=10, color=INK)\ncbar.ax.tick_params(labelsize=8, colors=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\n\n# Title — 44 chars, ratio=1.0, fontsize=12pt (at or below 67-char baseline)\ntitle = \"hexbin-basic · python · seaborn · anyplot.ai\"\ntitle_fontsize = max(8, round(12 * (67 / len(title) if len(title) > 67 else 1.0)))\ng.figure.suptitle(title, fontsize=title_fontsize, color=INK, fontweight=\"medium\", y=0.975)\n\n# Save — no bbox_inches='tight' so canvas stays exactly 2400×2400 px\ng.figure.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}