{"spec_id":"span-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nspan-basic: Basic Span Plot (Highlighted Region)\nLibrary: matplotlib 3.11.1 | Python 3.13.14\nQuality: 88/100 | Updated: 2026-07-25\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\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\nBRAND = \"#009E73\"  # Imprint palette position 1 — first series\nC2 = \"#C475FD\"  # Imprint palette position 2\nC3 = \"#4467A3\"  # Imprint palette position 3\n\n# Data — stock prices with a simulated recession dip\nnp.random.seed(42)\ndates = np.arange(2004, 2016, 0.1)\n\nprice = 100 + np.cumsum(np.random.randn(len(dates)) * 1.5)\nrecession_mask = (dates >= 2008) & (dates < 2010)\nprice[recession_mask] -= np.linspace(0, 35, recession_mask.sum())\nprice[dates >= 2010] -= 35\nprice = price - price.min() + 70\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nax.plot(dates, price, linewidth=3, color=BRAND, label=\"Stock Price Index\")\n\n# Vertical span — recession period (2008–2009), with subtle edge lines to\n# mark its boundaries and set it apart from the horizontal span below\nax.axvspan(2008, 2010, alpha=0.22, color=C2, label=\"Recession Period\")\nax.axvline(2008, color=C2, alpha=0.6, linewidth=1.2)\nax.axvline(2010, color=C2, alpha=0.6, linewidth=1.2)\n\n# Horizontal span — risk zone (low values)\nax.axhspan(70, 95, alpha=0.22, color=C3, label=\"Risk Zone\")\n\n# Text labels inside each span (blended transforms keep them anchored to the\n# span regardless of the data range, avoiding overlap with the price line).\n# Styling is deliberately differentiated between the two span types: the\n# time-based span reads as italic prose, the threshold span as a bold\n# small-caps tag.\nax.text(\n    2009,\n    0.92,\n    \"2008–2010 Recession\",\n    transform=ax.get_xaxis_transform(),\n    rotation=90,\n    ha=\"center\",\n    va=\"top\",\n    fontsize=8,\n    style=\"italic\",\n    color=INK,\n    alpha=0.85,\n)\nax.text(\n    0.02,\n    82.5,\n    \"RISK ZONE\",\n    transform=ax.get_yaxis_transform(),\n    ha=\"left\",\n    va=\"center\",\n    fontsize=8,\n    fontweight=\"bold\",\n    color=INK,\n    alpha=0.85,\n)\n\n# Style\nax.set_xlabel(\"Year\", fontsize=10, color=INK)\nax.set_ylabel(\"Price Index\", fontsize=10, color=INK)\nax.set_title(\"span-basic · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"bold\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\n\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor spine in (\"left\", \"bottom\"):\n    ax.spines[spine].set_color(INK_SOFT)\n\nax.yaxis.grid(True, alpha=0.12, linewidth=0.8, color=INK)\n\nleg = ax.legend(fontsize=8, loc=\"upper left\")\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nleg.get_frame().set_alpha(1.0)\nplt.setp(leg.get_texts(), color=INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}