{"spec_id":"hexbin-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nhexbin-basic: Basic Hexbin Plot\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 91/100 | Created: 2026-05-29\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove the script's own directory from sys.path so it doesn't shadow the installed matplotlib package\n_script_dir = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p or \".\") != _script_dir]\n\nimport matplotlib.colors as mcolors\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import LinearSegmentedColormap\nfrom matplotlib.ticker import LogFormatterSciNotation\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 — brand green → blue (single-polarity density)\nimprint_seq = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#4467A3\"])\n\n# Data — simulated urban sensor readings with three density clusters\nnp.random.seed(42)\nn_points = 10000\n\ndowntown_x = np.random.randn(n_points // 2) * 1.5 + 2\ndowntown_y = np.random.randn(n_points // 2) * 1.5 + 2\nindustrial_x = np.random.randn(n_points // 3) * 1.0 - 2\nindustrial_y = np.random.randn(n_points // 3) * 1.0 - 1\nsuburb_x = np.random.randn(n_points // 6) * 0.8 + 1\nsuburb_y = np.random.randn(n_points // 6) * 0.8 - 2\n\nlongitude = np.concatenate([downtown_x, industrial_x, suburb_x])\nlatitude = np.concatenate([downtown_y, industrial_y, suburb_y])\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nhb = ax.hexbin(\n    longitude,\n    latitude,\n    gridsize=30,\n    cmap=imprint_seq,\n    mincnt=1,\n    linewidths=0.2,\n    edgecolors=PAGE_BG,\n    norm=mcolors.LogNorm(),\n)\n\n# Colorbar with LogFormatterSciNotation for cleaner log-scale tick labels\ncbar = fig.colorbar(hb, ax=ax, shrink=0.85, pad=0.02)\ncbar.set_label(\"Sensor Reading Count\", fontsize=10, color=INK)\ncbar.ax.yaxis.set_major_formatter(LogFormatterSciNotation())\ncbar.ax.tick_params(labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\ncbar.outline.set_linewidth(0.5)\ncbar.outline.set_edgecolor(INK_SOFT)\n\n# Style\nax.set_xlabel(\"Longitude (km)\", fontsize=10, color=INK)\nax.set_ylabel(\"Latitude (km)\", fontsize=10, color=INK)\n\ntitle = \"hexbin-basic · python · matplotlib · anyplot.ai\"\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK)\n\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"left\"].set_linewidth(0.5)\nax.spines[\"bottom\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_linewidth(0.5)\n\n# Cluster annotations — label the three density zones to aid data storytelling\n_ann_style = {\n    \"fontsize\": 7,\n    \"color\": INK_SOFT,\n    \"ha\": \"center\",\n    \"va\": \"center\",\n    \"bbox\": {\n        \"facecolor\": ELEVATED_BG,\n        \"edgecolor\": INK_SOFT,\n        \"alpha\": 0.85,\n        \"boxstyle\": \"round,pad=0.3\",\n        \"linewidth\": 0.5,\n    },\n}\nax.text(2, 2, \"Downtown\", **_ann_style)\nax.text(-2, -1, \"Industrial District\", **_ann_style)\nax.text(1, -2, \"Suburban Area\", **_ann_style)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}