{"spec_id":"bifurcation-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nbifurcation-basic: Bifurcation Diagram for Dynamical Systems\nLibrary: plotnine 0.15.7 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-06-17\n\"\"\"\n\nimport os\nimport sys\n\n\n# Prevent this file (plotnine.py) from shadowing the installed plotnine package\nsys.path = [p for p in sys.path if p and not p.endswith(\"implementations\") and not p.endswith(\"/python\")]\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    annotate,\n    coord_cartesian,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_bin2d,\n    geom_vline,\n    ggplot,\n    guides,\n    labs,\n    scale_fill_gradient,\n    scale_x_continuous,\n    scale_y_continuous,\n    theme,\n    theme_minimal,\n)\n\n\n# Theme-adaptive chrome (Imprint 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 anchors used here\nBRAND = \"#009E73\"  # brand green — low end of the sequential density ramp\nBLUE = \"#4467A3\"  # imprint_seq high end\nANYPLOT_AMBER = \"#DDCC77\"  # warning / caution — flags the chaotic regime\n\n# Data — logistic map: x(n+1) = r * x(n) * (1 - x(n))\nr_values = np.linspace(2.5, 4.0, 4000)\nn_discard = 300\nn_keep = 150\n\nparameter = np.empty(r_values.size * n_keep)\nstate = np.empty(r_values.size * n_keep)\n\nx0 = 0.5\nidx = 0\nfor r in r_values:\n    x = x0\n    for _ in range(n_discard):\n        x = r * x * (1.0 - x)\n    for _ in range(n_keep):\n        x = r * x * (1.0 - x)\n        parameter[idx] = r\n        state[idx] = x\n        idx += 1\n\ndf = pd.DataFrame({\"parameter\": parameter, \"state\": state})\n\n# Key period-doubling bifurcation points of the logistic map\nbifurcation_r = [3.0, 3.449, 3.544]\n\n# Plot — geom_bin2d (2D density histogram) is the distinctive plotnine read on\n# this spec. A log-scaled imprint_seq fill keeps the sparse chaotic bins visible\n# instead of washing them out against the dense fixed-point/period branches.\nplot = (\n    ggplot(df, aes(x=\"parameter\", y=\"state\"))\n    + geom_bin2d(bins=(440, 280), na_rm=True)\n    + scale_fill_gradient(low=BRAND, high=BLUE, trans=\"log10\")\n    + geom_vline(xintercept=bifurcation_r, linetype=\"dashed\", color=INK_SOFT, alpha=0.7, size=0.4)\n    + annotate(\"text\", x=2.98, y=0.95, label=\"Period-2\\nr ≈ 3.0\", size=3.4, color=INK, ha=\"right\", fontweight=\"bold\")\n    + annotate(\"text\", x=3.435, y=0.95, label=\"Period-4\\nr ≈ 3.449\", size=3.4, color=INK, ha=\"right\", fontweight=\"bold\")\n    + annotate(\"text\", x=3.56, y=0.06, label=\"Period-8\\nr ≈ 3.544\", size=3.4, color=INK, ha=\"left\", fontweight=\"bold\")\n    + annotate(\n        \"label\",\n        x=2.62,\n        y=0.74,\n        label=\"Stable\\nFixed Point\",\n        size=3.4,\n        color=BRAND,\n        fontweight=\"bold\",\n        fill=ELEVATED_BG,\n        label_size=0,\n    )\n    + annotate(\n        \"label\", x=3.86, y=0.12, label=\"Chaos\", size=3.6, color=INK, fontweight=\"bold\", fill=ANYPLOT_AMBER, label_size=0\n    )\n    + labs(\n        x=\"Growth Rate (r)\", y=\"Steady-State Population (x)\", title=\"bifurcation-basic · python · plotnine · anyplot.ai\"\n    )\n    + scale_x_continuous(breaks=np.arange(2.5, 4.1, 0.25), expand=(0, 0))\n    + scale_y_continuous(breaks=np.arange(0, 1.1, 0.2), expand=(0, 0))\n    + coord_cartesian(xlim=(2.5, 4.0), ylim=(0, 1.0))\n    + guides(fill=False)\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        plot_title=element_text(size=12, weight=\"bold\", color=INK),\n        axis_title=element_text(size=10, color=INK),\n        axis_text=element_text(size=8, color=INK_SOFT),\n        panel_grid_major=element_line(color=INK_MUTED, size=0.25, alpha=0.18),\n        panel_grid_minor=element_blank(),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG, color=\"none\"),\n        legend_position=\"none\",\n    )\n)\n\n# Save (3200 x 1800 px)\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\", verbose=False)\n"}