{"spec_id":"stem-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nstem-basic: Basic Stem Plot\nLibrary: matplotlib 3.11.1 | Python 3.13.14\nQuality: 96/100 | Updated: 2026-07-25\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\n# Theme tokens (see prompts/default-style-guide.md \"Background\" + \"Theme-adaptive Chrome\")\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\"\nPOSITIVE = \"#009E73\"  # Imprint palette position 1 — semantic exception: positive polarity\nNEGATIVE = \"#AE3030\"  # Imprint palette position 5 — semantic exception: negative polarity\n\n# Data - discrete damped oscillation signal\nnp.random.seed(42)\nsample_index = np.arange(0, 30)\ndecay = np.exp(-sample_index / 10)\namplitude = decay * np.cos(sample_index * 0.8) + np.random.randn(30) * 0.05\nis_positive = amplitude >= 0\n\n# Plot — see default-style-guide.md \"Visual Sizing Defaults\" for the canvas + sizing values\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Decay envelope — matplotlib-distinctive overlay tracing the exponential bound\n# that governs the oscillation, drawn on a fine grid for a smooth dashed curve.\nenvelope_x = np.linspace(0, 29, 300)\nenvelope_y = np.exp(-envelope_x / 10)\nax.plot(envelope_x, envelope_y, linestyle=\"--\", linewidth=1.2, color=INK_SOFT, alpha=0.6, zorder=1)\nax.plot(envelope_x, -envelope_y, linestyle=\"--\", linewidth=1.2, color=INK_SOFT, alpha=0.6, zorder=1)\n\nmarkerline_pos, stemlines_pos, _ = ax.stem(sample_index[is_positive], amplitude[is_positive], basefmt=\" \")\nmarkerline_neg, stemlines_neg, _ = ax.stem(sample_index[~is_positive], amplitude[~is_positive], basefmt=\" \")\nax.axhline(0, linewidth=1.5, color=INK_SOFT, zorder=1)\n\nplt.setp(stemlines_pos, linewidth=2, color=POSITIVE, alpha=0.8, zorder=2)\nplt.setp(markerline_pos, markersize=12, color=POSITIVE, markeredgecolor=PAGE_BG, markeredgewidth=2, zorder=3)\nplt.setp(stemlines_neg, linewidth=2, color=NEGATIVE, alpha=0.8, zorder=2)\nplt.setp(markerline_neg, markersize=12, color=NEGATIVE, markeredgecolor=PAGE_BG, markeredgewidth=2, zorder=3)\n\n# Peak annotation — calls out the decay envelope's starting amplitude at n=0\nax.annotate(\n    f\"Peak {amplitude[0]:.2f} V\",\n    xy=(sample_index[0], amplitude[0]),\n    xytext=(sample_index[0] + 6, amplitude[0] - 0.35),\n    fontsize=8,\n    color=INK,\n    arrowprops={\"arrowstyle\": \"->\", \"color\": INK_SOFT, \"linewidth\": 1.2},\n    bbox={\"boxstyle\": \"round,pad=0.3\", \"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.9},\n    zorder=4,\n)\n\n# Style\ntitle = \"stem-basic · python · matplotlib · anyplot.ai\"\nax.set_xlabel(\"Sample Index (n)\", fontsize=10, color=INK)\nax.set_ylabel(\"Amplitude (V)\", fontsize=10, color=INK)\nax.set_title(title, fontsize=11, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\nax.yaxis.grid(True, alpha=0.12, linewidth=0.8, color=INK)\n\n# Save\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)  # bbox_inches MUST stay default (None)\n"}