Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                

Bifurcation Diagram for Dynamical Systems in lets-plot (Python)

The same plot in 14 other libraries — Python: Altair, Bokeh, Matplotlib, Plotly, plotnine, Pygal, Seaborn; R: ggplot2; Julia: Makie.jl; JavaScript: Chart.js, D3.js, Apache ECharts, Highcharts, MUI X Charts. Compare all 15 side by side: Bifurcation Diagram for Dynamical Systems in Python, R, Julia and JavaScript.

A bifurcation diagram shows how the steady-state behavior of a dynamical system changes as a control parameter varies. By plotting the long-term values of a state variable against a continuously varied parameter, it reveals transitions from stable fixed points through period-doubling cascades to chaotic regimes. The classic example is the logistic map, where the route to chaos is clearly visible as the growth rate parameter increases.

Bifurcation Diagram for Dynamical Systems rendered with lets-plot

Renders

Python source (lets-plot)

""" anyplot.ai
bifurcation-basic: Bifurcation Diagram for Dynamical Systems
Library: letsplot 4.11.0 | Python 3.14.10
Quality: 88/100 | Updated: 2026-12-01
"""

import os

import numpy as np
import pandas as pd
from lets_plot import (
    LetsPlot,
    aes,
    coord_cartesian,
    element_blank,
    element_line,
    element_rect,
    element_text,
    geom_point,
    geom_segment,
    geom_text,
    ggplot,
    ggsave,
    ggsize,
    labs,
    sampling_pick,
    scale_x_continuous,
    scale_y_continuous,
    theme,
    theme_minimal,
)


LetsPlot.setup_html()

THEME = os.getenv("ANYPLOT_THEME", "light")

# Theme-adaptive chrome tokens
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F"
GRID_COLOR = "rgba(26, 26, 23, 0.15)" if THEME == "light" else "rgba(240, 239, 232, 0.15)"

# Imprint palette (positions 1-8)
IMPRINT_PALETTE = ["#009E73", "#C475FD", "#4467A3", "#BD8233", "#AE3030", "#2ABCCD", "#954477", "#99B314"]

# Constants
BIFURCATIONS = {"Period-2": 3.0, "Period-4": 3.449, "Period-8": 3.544, "Chaos": 3.5699}  # known logistic-map splits
LABEL_GAP = 0.006  # distance between a marker line and its label, in r units

R_PARAM = "Growth Rate (r)"  # role: parameter
POPULATION = "Population (x)"  # role: state

# Data: replace this block with your own table, for example df = pd.read_csv("orbits.csv")
# Logistic map: x(n+1) = r * x(n) * (1 - x(n)); denser sampling in the chaotic regime
r_values = np.concatenate([np.linspace(2.5, 3.45, 600), np.linspace(3.45, 4.0, 1600)])
transient = 250
iterations = 100

r_all = []
x_all = []
for r in r_values:
    x = 0.5
    for _ in range(transient):
        x = r * x * (1.0 - x)
    for _ in range(iterations):
        x = r * x * (1.0 - x)
        r_all.append(r)
        x_all.append(x)

df = pd.DataFrame({R_PARAM: r_all, POPULATION: x_all})

# Plot
r_min, r_max = df[R_PARAM].min(), df[R_PARAM].max()
x_min, x_max = 0.0, 1.0  # the map's state space

# The Period-4 line stops below the Period-8 label, which is wider than the gap between the two lines
line_tops = [x_max, x_max - 0.07, x_max, x_max]
segments_df = pd.DataFrame({R_PARAM: list(BIFURCATIONS.values()), "ymin": x_min, "ymax": line_tops})

# Labels sit beside their marker line at one height above the data: left of the line,
# except the last (chaos onset), which has no room on its left and goes right
labels_df = pd.DataFrame(
    {
        R_PARAM: list(BIFURCATIONS.values()),
        POPULATION: [x_max - 0.07, x_max - 0.11, x_max - 0.055, x_max - 0.07],
        "label": list(BIFURCATIONS),
    }
)
labels_left = labels_df.iloc[:-1].copy()
labels_left[R_PARAM] -= LABEL_GAP
labels_right = labels_df.iloc[-1:].copy()
labels_right[R_PARAM] += LABEL_GAP

plot = (
    ggplot(df, aes(x=R_PARAM, y=POPULATION))
    + geom_point(size=0.4, alpha=0.35, color=IMPRINT_PALETTE[0], tooltips="none", sampling=sampling_pick(n=len(df)))
    + geom_segment(
        aes(x=R_PARAM, y="ymin", xend=R_PARAM, yend="ymax"),
        data=segments_df,
        color=INK_SOFT,
        size=0.3,
        linetype="dashed",
        inherit_aes=False,
        tooltips="none",
    )
    + geom_text(
        aes(x=R_PARAM, y=POPULATION, label="label"),
        data=labels_left,
        size=5,
        color=INK,
        hjust=1,
        vjust=0,
        inherit_aes=False,
    )
    + geom_text(
        aes(x=R_PARAM, y=POPULATION, label="label"),
        data=labels_right,
        size=5,
        color=INK,
        hjust=0,
        vjust=0,
        inherit_aes=False,
    )
    + labs(
        x=R_PARAM,
        y=POPULATION,
        title="bifurcation-basic · python · letsplot · anyplot.ai",
        caption="Logistic map: x(n+1) = r · x(n) · (1 − x(n))",
    )
    + scale_x_continuous(breaks=np.linspace(r_min, r_max, 7).tolist(), expand=[0.02, 0], format=".2f")
    + scale_y_continuous(breaks=np.linspace(0, 1, 6).tolist(), expand=[0.02, 0], format=".1f")
    + coord_cartesian(ylim=[0, 1])
    # Canvas: ggsize(800, 450) × scale=4 → 3200×1800 px (landscape)
    + ggsize(800, 450)
    + theme_minimal()
    + theme(
        axis_text=element_text(size=10, color=INK_SOFT),
        axis_title=element_text(size=12, color=INK),
        plot_title=element_text(size=16, color=INK, face="bold"),
        plot_caption=element_text(size=9, color=INK_MUTED, face="italic"),
        panel_grid_major_x=element_line(color=GRID_COLOR, size=0.2),
        panel_grid_major_y=element_line(color=GRID_COLOR, size=0.15),
        panel_grid_minor=element_blank(),
        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
        panel_background=element_rect(fill=PAGE_BG, size=0),
        axis_ticks=element_line(color=INK_SOFT, size=0.3),
        axis_line=element_line(color=INK_SOFT, size=0.4),
        plot_margin=[30, 40, 20, 20],
    )
)

# Save PNG (scale=4 → 3200×1800 px) and interactive HTML
ggsave(plot, f"plot-{THEME}.png", path=".", scale=4)
ggsave(plot, f"plot-{THEME}.html", path=".")

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/bifurcation-basic/letsplot/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.

{
  "spec_id": "bifurcation-basic",
  "language": "python",
  "library": "letsplot",
  "page": "https://anyplot.ai/bifurcation-basic/python/letsplot",
  "hub": "https://anyplot.ai/bifurcation-basic",
  "code_json": "https://api.anyplot.ai/specs/bifurcation-basic/letsplot/code",
  "spec_json": "https://api.anyplot.ai/specs/bifurcation-basic",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/bifurcation-basic/python/letsplot/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/bifurcation-basic/python/letsplot/plot-dark.png",
  "interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/bifurcation-basic/python/letsplot/plot-light.html",
  "interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/bifurcation-basic/python/letsplot/plot-dark.html",
  "quality_score": 88.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Bifurcation Diagram for Dynamical Systems on anyplot.ai.

Other implementations