{"spec_id":"tree-phylogenetic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\ntree-phylogenetic: Phylogenetic Tree Diagram\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 82/100 | Updated: 2026-05-15\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Patch\n\n\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# Clade colors (excellent colorblind-safe palette - preserved from previous implementation)\nAPES_COLOR = \"#306998\"\nMONKEYS_COLOR = \"#FFD43B\"\nPROSIMIANS_COLOR = \"#5A9C5A\"\n\n# Primate phylogenetic tree - pre-computed coordinates\n# Each leaf: (name, y_position, x_end) where x_end is total distance from root\n# Species ordered by evolutionary grouping (top to bottom)\nspecies = [\n    (\"Galago\", 0, 0.24),\n    (\"Tarsier\", 1, 0.24),\n    (\"Lemur\", 2, 0.18),\n    (\"Spider Monkey\", 3, 0.20),\n    (\"Capuchin\", 4, 0.19),\n    (\"Baboon\", 5, 0.18),\n    (\"Rhesus Macaque\", 6, 0.18),\n    (\"Gibbon\", 7, 0.16),\n    (\"Orangutan\", 8, 0.15),\n    (\"Gorilla\", 9, 0.14),\n    (\"Chimpanzee\", 10, 0.13),\n    (\"Human\", 11, 0.13),\n]\n\n# Pre-computed branch segments for the phylogenetic tree\n# Format: (x_start, y_start, x_end, y_end, color)\nbranches = [\n    # Root split (Prosimians vs Simians)\n    (0, 5.5, 0.05, 5.5, INK_SOFT),  # Root to split\n    (0.05, 1, 0.05, 5.5, INK_SOFT),  # Vertical at root\n    (0.05, 1, 0.05, 10, INK_SOFT),  # Vertical to Simians\n    # Prosimians clade\n    (0.05, 1, 0.08, 1, PROSIMIANS_COLOR),  # To Prosimians node\n    (0.08, 0.5, 0.08, 2, PROSIMIANS_COLOR),  # Prosimians vertical\n    (0.08, 0.5, 0.10, 0.5, PROSIMIANS_COLOR),  # To Lorisoids\n    (0.10, 0, 0.10, 1, PROSIMIANS_COLOR),  # Lorisoids vertical\n    (0.10, 0, 0.24, 0, PROSIMIANS_COLOR),  # To Galago\n    (0.10, 1, 0.24, 1, PROSIMIANS_COLOR),  # To Tarsier\n    (0.08, 2, 0.18, 2, PROSIMIANS_COLOR),  # To Lemur\n    # Simians main branch\n    (0.05, 10, 0.08, 10, INK_SOFT),  # To Simians node\n    (0.08, 5.5, 0.08, 10, INK_SOFT),  # Simians vertical\n    # Apes clade\n    (0.08, 10, 0.10, 10, APES_COLOR),  # To Apes\n    (0.10, 8.5, 0.10, 10, APES_COLOR),  # Apes vertical\n    # African Apes\n    (0.10, 10, 0.11, 10, APES_COLOR),  # To African Apes\n    (0.11, 9.5, 0.11, 11, APES_COLOR),  # African Apes vertical\n    (0.11, 9, 0.14, 9, APES_COLOR),  # To Gorilla\n    (0.11, 10.5, 0.12, 10.5, APES_COLOR),  # To Hominini\n    (0.12, 10, 0.12, 11, APES_COLOR),  # Hominini vertical\n    (0.12, 10, 0.13, 10, APES_COLOR),  # To Chimpanzee\n    (0.12, 11, 0.13, 11, APES_COLOR),  # To Human\n    # Asian Apes\n    (0.10, 8.5, 0.13, 8.5, APES_COLOR),  # To Asian Apes\n    (0.13, 7, 0.13, 8, APES_COLOR),  # Asian Apes vertical\n    (0.13, 7, 0.16, 7, APES_COLOR),  # To Gibbon\n    (0.13, 8, 0.15, 8, APES_COLOR),  # To Orangutan\n    # Monkeys clade\n    (0.08, 5.5, 0.12, 5.5, MONKEYS_COLOR),  # To Monkeys\n    (0.12, 3.5, 0.12, 5.5, MONKEYS_COLOR),  # Monkeys vertical\n    # Old World Monkeys\n    (0.12, 5.5, 0.14, 5.5, MONKEYS_COLOR),  # To Old World\n    (0.14, 5, 0.14, 6, MONKEYS_COLOR),  # OW vertical\n    (0.14, 5, 0.18, 5, MONKEYS_COLOR),  # To Baboon\n    (0.14, 6, 0.18, 6, MONKEYS_COLOR),  # To Rhesus\n    # New World Monkeys\n    (0.12, 3.5, 0.15, 3.5, MONKEYS_COLOR),  # To New World\n    (0.15, 3, 0.15, 4, MONKEYS_COLOR),  # NW vertical\n    (0.15, 3, 0.20, 3, MONKEYS_COLOR),  # To Spider Monkey\n    (0.15, 4, 0.19, 4, MONKEYS_COLOR),  # To Capuchin\n]\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Draw all branches\nfor x1, y1, x2, y2, color in branches:\n    ax.plot([x1, x2], [y1, y2], color=color, linewidth=2.5, solid_capstyle=\"round\")\n\n# Draw species labels\nfor name, y, x in species:\n    ax.plot(x, y, \"o\", color=INK_SOFT, markersize=8)\n    ax.text(x + 0.008, y, name, va=\"center\", ha=\"left\", fontsize=16, fontweight=\"medium\", color=INK)\n\n# Style\nax.set_xlabel(\"Evolutionary Distance (substitutions per site)\", fontsize=20, color=INK)\nax.set_ylabel(\"\")\nax.set_title(\n    \"Primate Evolution · tree-phylogenetic · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK\n)\n\n# Set axis limits\nax.set_xlim(-0.02, 0.38)\nax.set_ylim(-1, 12.5)\n\n# Style ticks\nax.tick_params(axis=\"x\", labelsize=16, colors=INK_SOFT, labelcolor=INK_SOFT)\nax.tick_params(axis=\"y\", left=False, labelleft=False)\n\n# Add subtle grid on x-axis only\nax.grid(True, axis=\"x\", alpha=0.15, linestyle=\"-\", linewidth=0.8, color=INK_SOFT)\n\n# Remove spines\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_visible(False)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\n# Add legend explaining clade colors\nlegend_elements = [\n    Patch(facecolor=APES_COLOR, edgecolor=INK_SOFT, label=\"Apes\"),\n    Patch(facecolor=MONKEYS_COLOR, edgecolor=INK_SOFT, label=\"Monkeys\"),\n    Patch(facecolor=PROSIMIANS_COLOR, edgecolor=INK_SOFT, label=\"Prosimians\"),\n]\nleg = ax.legend(\n    handles=legend_elements, loc=\"upper left\", fontsize=16, frameon=True, fancybox=False, edgecolor=INK_SOFT\n)\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nleg.get_frame().set_linewidth(1.5)\nfor text in leg.get_texts():\n    text.set_color(INK_SOFT)\n\n# Add scale bar\nax.plot([0, 0.05], [-0.5, -0.5], color=INK_SOFT, linewidth=3, solid_capstyle=\"butt\")\nax.text(0.025, -0.8, \"0.05\", ha=\"center\", va=\"top\", fontsize=14, color=INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}