{"spec_id":"density-rug","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\ndensity-rug: Density Plot with Rug Marks\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\nfrom scipy.stats import gaussian_kde\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.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1\n\n# Data - Response times for a web application (in milliseconds)\nnp.random.seed(42)\n# Create a bimodal distribution to show interesting KDE behavior\nresponse_times = np.concatenate(\n    [\n        np.random.normal(120, 25, 80),  # Fast responses\n        np.random.normal(250, 40, 40),  # Slower responses\n    ]\n)\n\n# Compute KDE\nkde = gaussian_kde(response_times)\nx_range = np.linspace(response_times.min() - 30, response_times.max() + 30, 500)\ndensity = kde(x_range)\n\n# Create figure\nfig = go.Figure()\n\n# Add filled KDE curve with brand color\nfig.add_trace(\n    go.Scatter(\n        x=x_range,\n        y=density,\n        mode=\"lines\",\n        fill=\"tozeroy\",\n        fillcolor=f\"rgba({int(BRAND[1:3], 16)}, {int(BRAND[3:5], 16)}, {int(BRAND[5:7], 16)}, 0.35)\",\n        line=dict(color=BRAND, width=3),\n        name=\"Density\",\n        hovertemplate=\"Time: %{x:.1f} ms<br>Density: %{y:.4f}<extra></extra>\",\n    )\n)\n\n# Add rug marks at y=0\nrug_height = max(density) * 0.04\nfig.add_trace(\n    go.Scatter(\n        x=response_times,\n        y=np.zeros_like(response_times) - rug_height * 0.5,\n        mode=\"markers\",\n        marker=dict(\n            symbol=\"line-ns\",\n            size=14,\n            line=dict(width=2, color=f\"rgba({int(BRAND[1:3], 16)}, {int(BRAND[3:5], 16)}, {int(BRAND[5:7], 16)}, 0.6)\"),\n            color=f\"rgba({int(BRAND[1:3], 16)}, {int(BRAND[3:5], 16)}, {int(BRAND[5:7], 16)}, 0.6)\",\n        ),\n        name=\"Observations\",\n        hovertemplate=\"Response Time: %{x:.1f} ms<extra></extra>\",\n    )\n)\n\n# Layout with theme-adaptive colors\nfig.update_layout(\n    title=dict(\n        text=\"density-rug · Python · plotly · anyplot.ai\", font=dict(size=28, color=INK), x=0.5, xanchor=\"center\"\n    ),\n    xaxis=dict(\n        title=dict(text=\"Response Time (ms)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        gridwidth=1,\n        zeroline=False,\n        showline=True,\n        linewidth=2,\n        linecolor=INK_SOFT,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Density\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        gridwidth=1,\n        zeroline=False,\n        showline=True,\n        linewidth=2,\n        linecolor=INK_SOFT,\n        rangemode=\"tozero\",\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font=dict(color=INK),\n    showlegend=True,\n    legend=dict(\n        font=dict(size=16, color=INK_SOFT),\n        x=0.02,\n        y=0.98,\n        xanchor=\"left\",\n        yanchor=\"top\",\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n    ),\n    margin=dict(l=100, r=60, t=100, b=100),\n)\n\n# Adjust y-axis range to include rug marks below zero\nfig.update_yaxes(range=[-rug_height * 1.5, max(density) * 1.08])\n\n# Save as PNG (4800 x 2700 px)\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\n\n# Save interactive HTML\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}