{"spec_id":"rug-basic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nrug-basic: Basic Rug Plot\nLibrary: plotly 6.9.0 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-07-25\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\nfrom scipy import stats\n\n\n# Theme 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\"\nGRID = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\nBRAND = \"#009E73\"  # Imprint palette position 1\n\n# Data — trimodal server latency distribution: cache hit, computed response,\n# cold start. Three well-separated clusters expose two distinct gaps that a\n# rug plot's individual tick marks reveal far better than a coarse histogram.\nnp.random.seed(42)\ncache_hit = np.random.normal(loc=20, scale=3, size=60)\ncomputed = np.random.normal(loc=60, scale=6, size=55)\ncold_start = np.random.normal(loc=100, scale=9, size=35)\nvalues = np.concatenate([cache_hit, computed, cold_start])\n\n# KDE density curve\nx_kde = np.linspace(values.min() - 5, values.max() + 5, 400)\nkde = stats.gaussian_kde(values, bw_method=\"scott\")\ndensity = kde(x_kde)\nrug_y = np.full_like(values, -density.max() * 0.06)\n\n# Figure\nfig = go.Figure()\n\n# Filled KDE density curve\nfig.add_trace(\n    go.Scatter(\n        x=x_kde,\n        y=density,\n        mode=\"lines\",\n        line=dict(color=BRAND, width=2.5),\n        fill=\"tozeroy\",\n        fillcolor=\"rgba(0,158,115,0.15)\",\n        name=\"Density (KDE)\",\n        hovertemplate=\"Response Time: %{x:.1f} ms<br>Density: %{y:.4f}<extra></extra>\",\n    )\n)\n\n# Rug ticks — individual observations as vertical marks below the x-axis\nfig.add_trace(\n    go.Scatter(\n        x=values,\n        y=rug_y,\n        mode=\"markers\",\n        marker=dict(symbol=\"line-ns\", size=20, line=dict(width=1.5, color=BRAND), color=BRAND),\n        opacity=0.5,\n        name=\"Observations\",\n        hovertemplate=\"Response Time: %{x:.2f} ms<extra></extra>\",\n    )\n)\n\n# Gap annotations — highlight the empty regions separating the three clusters.\n# Anchored near the rug baseline with text pushed well above the curve peaks\n# so the label never overlaps the KDE line or its fill.\nfig.add_annotation(\n    x=38,\n    y=0,\n    text=\"gap ~30-45 ms\",\n    showarrow=True,\n    arrowhead=2,\n    arrowcolor=INK_SOFT,\n    font=dict(size=11, color=INK_SOFT),\n    ax=0,\n    ay=-150,\n)\nfig.add_annotation(\n    x=80,\n    y=0,\n    text=\"gap ~72-88 ms\",\n    showarrow=True,\n    arrowhead=2,\n    arrowcolor=INK_SOFT,\n    font=dict(size=11, color=INK_SOFT),\n    ax=0,\n    ay=-150,\n)\n\n# Layout\nfig.update_layout(\n    autosize=False,\n    title=dict(text=\"rug-basic · plotly · anyplot.ai\", font=dict(size=16, color=INK), x=0.5, xanchor=\"center\"),\n    xaxis=dict(\n        title=dict(text=\"Response Time (ms)\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        showgrid=True,\n        gridcolor=GRID,\n        gridwidth=1,\n        zeroline=False,\n        linecolor=INK_SOFT,\n        showline=True,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Density\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        showgrid=False,\n        zeroline=True,\n        zerolinecolor=INK_SOFT,\n        zerolinewidth=1,\n        linecolor=INK_SOFT,\n        showline=True,\n        range=[-density.max() * 0.15, density.max() * 1.15],\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font=dict(color=INK),\n    legend=dict(\n        bgcolor=ELEVATED_BG, bordercolor=INK_SOFT, borderwidth=1, font=dict(size=10, color=INK_SOFT), x=0.78, y=0.95\n    ),\n    margin=dict(l=70, r=40, t=70, b=55),\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"}