{"spec_id":"line-reaction-coordinate","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nline-reaction-coordinate: Reaction Coordinate Energy Diagram\nLibrary: matplotlib 3.11.0 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-06-24\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove the script's own directory from sys.path so that `import matplotlib`\n# resolves to the installed package rather than this file (which is named matplotlib.py).\nsys.path.pop(0)\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.patches import FancyArrowPatch\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# Imprint palette — position 1 for main curve; semantic red for barrier\nBRAND = \"#009E73\"  # Imprint palette position 1 — energy pathway curve\nEA_COLOR = \"#AE3030\"  # Imprint matte red — semantic: activation barrier (cost)\nDH_COLOR = \"#4467A3\"  # Imprint blue — thermodynamic enthalpy change\n\n# Data — single-step exothermic reaction\nreactant_energy = 50.0\ntransition_energy = 120.0\nproduct_energy = 20.0\npeak_pos = 0.45\n\nreaction_coord = np.linspace(0, 1, 500)\nsigma = 0.13\nbaseline = reactant_energy + (product_energy - reactant_energy) * (3 * reaction_coord**2 - 2 * reaction_coord**3)\ngaussian_bump = (transition_energy - (reactant_energy + product_energy) / 2) * np.exp(\n    -((reaction_coord - peak_pos) ** 2) / (2 * sigma**2)\n)\nenergy = baseline + gaussian_bump\n\npeak_idx = np.argmax(energy)\nactual_peak = energy[peak_idx]\npeak_x = reaction_coord[peak_idx]\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nax.plot(reaction_coord, energy, color=BRAND, linewidth=3.5, zorder=3)\nax.fill_between(reaction_coord, 0, energy, color=BRAND, alpha=0.09, zorder=2)\n\n# Dashed reference lines at reactant and product energy levels\nax.hlines(reactant_energy, -0.05, 0.20, colors=INK_MUTED, linestyles=\"--\", linewidth=1.2, alpha=0.7)\nax.hlines(reactant_energy, 0.80, 1.08, colors=INK_MUTED, linestyles=\"--\", linewidth=1.2, alpha=0.7)\nax.hlines(product_energy, 0.76, 1.08, colors=INK_MUTED, linestyles=\"--\", linewidth=1.2, alpha=0.7)\n\n# Transition state marker\nax.scatter([peak_x], [actual_peak], s=120, color=EA_COLOR, zorder=4, edgecolors=PAGE_BG, linewidth=1.5)\n\n# Direct labels for reactants, products, transition state\nax.text(0.02, reactant_energy + 3, \"Reactants\\n(50 kJ/mol)\", fontsize=8, fontweight=\"medium\", color=INK, va=\"bottom\")\nax.text(0.77, product_energy - 3, \"Products\\n(20 kJ/mol)\", fontsize=8, fontweight=\"medium\", color=INK, va=\"top\")\nax.text(\n    peak_x,\n    actual_peak + 4,\n    \"Transition State\",\n    fontsize=8,\n    fontweight=\"medium\",\n    color=EA_COLOR,\n    va=\"bottom\",\n    ha=\"center\",\n)\n\n# Activation energy arrow (Ea) — vertical double-headed arrow from reactant level to TS\nea_x = 0.10\narrow_ea = FancyArrowPatch(\n    (ea_x, reactant_energy),\n    (ea_x, actual_peak),\n    arrowstyle=\"<->\",\n    mutation_scale=14,\n    linewidth=1.8,\n    color=EA_COLOR,\n    zorder=5,\n)\nax.add_patch(arrow_ea)\nea_value = actual_peak - reactant_energy\nax.text(\n    ea_x + 0.025,\n    (reactant_energy + actual_peak) / 2,\n    f\"$E_a$ = {ea_value:.0f} kJ/mol\",\n    fontsize=8,\n    fontweight=\"bold\",\n    color=EA_COLOR,\n    ha=\"left\",\n    va=\"center\",\n    bbox={\"boxstyle\": \"round,pad=0.25\", \"facecolor\": ELEVATED_BG, \"edgecolor\": \"none\", \"alpha\": 0.9},\n)\n\n# Enthalpy change arrow (ΔH) — vertical double-headed arrow on right\ndh_x = 1.02\narrow_dh = FancyArrowPatch(\n    (dh_x, reactant_energy),\n    (dh_x, product_energy),\n    arrowstyle=\"<->\",\n    mutation_scale=14,\n    linewidth=1.8,\n    color=DH_COLOR,\n    zorder=5,\n)\nax.add_patch(arrow_dh)\ndh_value = product_energy - reactant_energy\nax.text(\n    dh_x + 0.02,\n    (reactant_energy + product_energy) / 2,\n    f\"$\\\\Delta H$ = {dh_value:.0f} kJ/mol\",\n    fontsize=8,\n    fontweight=\"bold\",\n    color=DH_COLOR,\n    ha=\"left\",\n    va=\"center\",\n    bbox={\"boxstyle\": \"round,pad=0.25\", \"facecolor\": ELEVATED_BG, \"edgecolor\": \"none\", \"alpha\": 0.9},\n)\n\n# Style\ntitle = \"line-reaction-coordinate · python · matplotlib · anyplot.ai\"\nax.set_xlabel(\"Reaction Coordinate\", fontsize=10, color=INK)\nax.set_ylabel(\"Potential Energy (kJ/mol)\", fontsize=10, color=INK)\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\nax.set_xlim(-0.05, 1.22)\nax.set_ylim(0, actual_peak + 22)\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.15, linewidth=0.8, color=INK)\nax.set_xticks([])\n\nfig.subplots_adjust(left=0.10, right=0.97, top=0.91, bottom=0.11)\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}