{"spec_id":"bifurcation-basic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nbifurcation-basic: Bifurcation Diagram for Dynamical Systems\nLibrary: plotly 6.8.0 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-06-17\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme tokens (Imprint palette + 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\"\nGRID = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — always first series\n\n# Data: Logistic map x(n+1) = r * x(n) * (1 - x(n))\nr_values = np.linspace(2.5, 4.0, 2000)\ntransient = 200\niterations = 100\n\nr_all = []\nx_all = []\n\nfor r in r_values:\n    x = 0.5\n    for _ in range(transient):\n        x = r * x * (1.0 - x)\n    for _ in range(iterations):\n        x = r * x * (1.0 - x)\n        r_all.append(r)\n        x_all.append(x)\n\nr_all = np.array(r_all)\nx_all = np.array(x_all)\n\n# Plot\nfig = go.Figure()\n\nfig.add_trace(\n    go.Scattergl(\n        x=r_all,\n        y=x_all,\n        mode=\"markers\",\n        marker={\"size\": 1, \"color\": BRAND, \"opacity\": 0.18},\n        showlegend=False,\n        hovertemplate=\"r = %{x:.4f}<br>x = %{y:.4f}<extra></extra>\",\n    )\n)\n\n# Subtle shading to emphasise the chaotic regime (r > 3.5699)\nfig.add_shape(\n    type=\"rect\", x0=3.5699, x1=4.05, y0=-0.05, y1=1.05, fillcolor=BRAND, opacity=0.04, line={\"width\": 0}, layer=\"below\"\n)\n\n# Key bifurcation points — vertical reference lines + annotations\n# Period-4 and Period-8 are only 0.095 r-units apart, so they're staggered\n# vertically to avoid overlap at 3200×1800.\nbifurcation_points = [\n    (3.0, \"Period-2\", \"center\", 1.04),\n    (3.449, \"Period-4\", \"center\", 1.04),\n    (3.544, \"Period-8\", \"right\", 1.22),\n    (3.5699, \"Chaos onset\", \"left\", 1.04),\n]\n\nannotations = []\nvline_color = \"rgba(174,48,48,0.40)\"  # Imprint matte red, subdued\n\nfor r_bif, label, xanchor, y_pos in bifurcation_points:\n    fig.add_vline(x=r_bif, line={\"color\": vline_color, \"width\": 1.5, \"dash\": \"dot\"})\n    annotations.append(\n        {\n            \"x\": r_bif,\n            \"y\": y_pos,\n            \"yref\": \"paper\",\n            \"text\": f\"<b>{label}</b><br>r ≈ {r_bif}\",\n            \"showarrow\": False,\n            \"font\": {\"size\": 11, \"color\": INK_SOFT, \"family\": \"Arial, sans-serif\"},\n            \"bgcolor\": ELEVATED_BG,\n            \"bordercolor\": INK_SOFT,\n            \"borderpad\": 4,\n            \"xanchor\": xanchor,\n        }\n    )\n\n# Title — scale fontsize if longer than 67-char baseline\ntitle = \"Logistic Map · bifurcation-basic · python · plotly · anyplot.ai\"\nn = len(title)\nratio = 67 / n if n > 67 else 1.0\ntitle_fontsize = max(11, round(16 * ratio))\n\n# Style\nfig.update_layout(\n    autosize=False,\n    title={\n        \"text\": title,\n        \"font\": {\"size\": title_fontsize, \"color\": INK, \"family\": \"Arial, sans-serif\"},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n        \"y\": 0.97,\n    },\n    xaxis={\n        \"title\": {\"text\": \"Growth Rate (r)\", \"font\": {\"size\": 12, \"color\": INK}, \"standoff\": 12},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"showgrid\": False,\n        \"showline\": True,\n        \"mirror\": False,\n        \"range\": [2.45, 4.05],\n        \"zeroline\": False,\n        \"dtick\": 0.25,\n        \"linecolor\": INK_SOFT,\n    },\n    yaxis={\n        \"title\": {\"text\": \"Steady-State Population (x)\", \"font\": {\"size\": 12, \"color\": INK}, \"standoff\": 12},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"showgrid\": True,\n        \"showline\": True,\n        \"mirror\": False,\n        \"gridwidth\": 1,\n        \"gridcolor\": GRID,\n        \"range\": [-0.05, 1.05],\n        \"zeroline\": False,\n        \"linecolor\": INK_SOFT,\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    showlegend=False,\n    margin={\"l\": 80, \"r\": 40, \"t\": 150, \"b\": 70},\n    annotations=annotations,\n    hoverlabel={\"bgcolor\": ELEVATED_BG, \"font_size\": 12, \"font_color\": INK},\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}