{"spec_id":"bifurcation-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nbifurcation-basic: Bifurcation Diagram for Dynamical Systems\nLibrary: bokeh 3.9.1 | Python 3.13.13\nQuality: 93/100 | Updated: 2026-06-17\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import HoverTool, Label, Range1d, Span\nfrom bokeh.plotting import ColumnDataSource, figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# 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\"\n\n# Imprint palette — regime encoding tells the route-to-chaos story.\n# Semantic ordering: stable=brand green, intermediate periods cool blue/lavender,\n# chaos=matte-red (Imprint semantic anchor for instability). First series #009E73.\nIMPRINT_STABLE = \"#009E73\"  # position 1 — stable fixed point (good/safe → green)\nIMPRINT_PERIOD2 = \"#4467A3\"  # position 3 — period-2 band\nIMPRINT_PERIOD4 = \"#C475FD\"  # position 2 — period-4 / higher period\nIMPRINT_CHAOS = \"#AE3030\"  # position 5 — chaotic regime (instability → red anchor)\n\n# Data — logistic map: x(n+1) = r * x(n) * (1 - x(n))\nr_min, r_max = 2.5, 4.0\nn_r = 3000\nn_transient = 300\nn_keep = 150\n\nr_values = np.linspace(r_min, r_max, n_r)\nall_r = np.repeat(r_values, n_keep)\nall_x = np.empty_like(all_r)\n\nidx = 0\nfor r in r_values:\n    x = 0.5\n    for _ in range(n_transient):\n        x = r * x * (1.0 - x)\n    for _ in range(n_keep):\n        x = r * x * (1.0 - x)\n        all_x[idx] = x\n        idx += 1\n\n# Vectorized regime-based color + density-aware alpha (np.select, no Python loop).\n# Chaos alpha lifted from the old 0.15 to 0.30 so the dense red fractal holds\n# contrast on both the cream and near-black surfaces.\nr_cuts = [all_r < 3.0, all_r < 3.449, all_r < 3.5699]\ncolors = np.select(r_cuts, [IMPRINT_STABLE, IMPRINT_PERIOD2, IMPRINT_PERIOD4], default=IMPRINT_CHAOS)\nalphas = np.select(r_cuts, [0.65, 0.50, 0.40], default=0.30)\n\nsource = ColumnDataSource(data={\"r\": all_r, \"x\": all_x, \"color\": colors, \"alpha\": alphas})\n\n# Plot — canonical 3200×1800 landscape canvas\ntitle = \"bifurcation-basic · python · bokeh · anyplot.ai\"\np = figure(\n    width=3200,\n    height=1800,\n    title=title,\n    x_axis_label=\"Growth Rate (r)\",\n    y_axis_label=\"Steady-State Population (x)\",\n    x_range=Range1d(r_min - 0.02, r_max + 0.02),\n    y_range=Range1d(-0.04, 1.04),\n    toolbar_location=None,\n    min_border_bottom=160,\n    min_border_left=180,\n    min_border_top=110,\n    min_border_right=60,\n    tools=\"pan,wheel_zoom,box_zoom,reset,save\",\n    active_scroll=\"wheel_zoom\",\n)\n\nscatter = p.scatter(x=\"r\", y=\"x\", source=source, size=2, color=\"color\", alpha=\"alpha\", line_color=None)\n\n# HoverTool — Bokeh-distinctive interactive feature (live in the HTML artifact)\nhover = HoverTool(\n    renderers=[scatter], tooltips=[(\"r\", \"@r{0.000}\"), (\"x\", \"@x{0.0000}\")], point_policy=\"snap_to_data\", mode=\"mouse\"\n)\np.add_tools(hover)\n\n# Vertical guides at the key bifurcation points\nbif_points = [3.0, 3.449, 3.5699]\nfor r_bif in bif_points:\n    p.add_layout(\n        Span(location=r_bif, dimension=\"height\", line_color=INK_SOFT, line_width=2, line_alpha=0.35, line_dash=\"dashed\")\n    )\n\n# Annotations — boxed so they read over the dense chaotic fan; the chaos label\n# now sits high inside the structure it describes instead of floating at y≈0.05.\nannotations = [\n    (3.0, 0.70, \"r ≈ 3.0\\nPeriod-2\", \"right\"),\n    (3.449, 0.90, \"r ≈ 3.449\\nPeriod-4\", \"right\"),\n    (3.5699, 0.97, \"r ≈ 3.57\\nOnset of chaos\", \"left\"),\n]\nfor r_bif, y_pos, label_text, align in annotations:\n    p.add_layout(\n        Label(\n            x=r_bif,\n            y=y_pos,\n            text=label_text,\n            text_font_size=\"30pt\",\n            text_font_style=\"bold\",\n            text_color=INK,\n            text_align=align,\n            x_offset=14 if align == \"left\" else -14,\n            background_fill_color=ELEVATED_BG,\n            background_fill_alpha=0.82,\n            border_line_color=INK_SOFT,\n            border_line_alpha=0.4,\n        )\n    )\n\n# Style — typography\np.title.text_font_size = \"50pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"42pt\"\np.yaxis.axis_label_text_font_size = \"42pt\"\np.xaxis.major_label_text_font_size = \"34pt\"\np.yaxis.major_label_text_font_size = \"34pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\n# Chrome — clean L-frame, ticks/axis lines removed for a minimal scatter\np.xaxis.axis_line_color = None\np.yaxis.axis_line_color = None\np.xaxis.major_tick_line_color = None\np.yaxis.major_tick_line_color = None\np.xaxis.minor_tick_line_color = None\np.yaxis.minor_tick_line_color = None\n\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.12\np.ygrid.grid_line_alpha = 0.12\n\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = None\n\np.xaxis.ticker.desired_num_ticks = 12\np.yaxis.ticker.desired_num_ticks = 8\n\n# Save — interactive HTML artifact + headless-Chrome screenshot at the exact canvas size\noutput_file(f\"plot-{THEME}.html\", title=\"Bifurcation Diagram\")\nsave(p)\n\nW, H = 3200, 1800\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)  # let bokeh's JS render the canvas\n\n# Headless Chrome reserves a strip of window chrome, so the inner viewport (what\n# save_screenshot captures) lands ~140px short of the requested window height.\n# Measure the delta and resize so the viewport is EXACTLY 3200×1800.\ninner_w, inner_h = driver.execute_script(\"return [window.innerWidth, window.innerHeight];\")\ndriver.set_window_size(W + (W - inner_w), H + (H - inner_h))\ntime.sleep(1)\n\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}