{"spec_id":"raincloud-basic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nraincloud-basic: Basic Raincloud Plot\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-26\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\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)\"\n\n# anyplot categorical palette — first series is always brand green\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data — reaction times (ms) across experimental conditions\nnp.random.seed(42)\nconditions = [\"Control\", \"Treatment A\", \"Treatment B\", \"Treatment C\"]\nn_per_group = 80\n\ndata = {\n    \"Control\": np.random.normal(450, 60, n_per_group),\n    \"Treatment A\": np.random.normal(380, 45, n_per_group),\n    \"Treatment B\": np.concatenate(\n        [np.random.normal(350, 30, n_per_group // 2), np.random.normal(480, 35, n_per_group // 2)]\n    ),\n    \"Treatment C\": np.random.normal(400, 80, n_per_group),\n}\ndata[\"Control\"] = np.append(data[\"Control\"], [620, 650, 280])\ndata[\"Treatment C\"] = np.append(data[\"Treatment C\"], [600, 620, 250])\n\n# X-axis range from data with padding\nall_values = np.concatenate(list(data.values()))\nx_min, x_max = all_values.min(), all_values.max()\nx_padding = (x_max - x_min) * 0.05\nx_range = [x_min - x_padding, x_max + x_padding]\n\n# Plot\nfig = go.Figure()\n\ncloud_width = 0.7\nbox_width = 0.10\nrain_offset = -0.22\nrain_jitter_amp = 0.08\n\nfor i, (condition, values) in enumerate(data.items()):\n    color = IMPRINT_PALETTE[i]\n    y_base = np.full(len(values), i)\n\n    # Cloud — half-violin extending upward above the category baseline\n    fig.add_trace(\n        go.Violin(\n            x=values,\n            y=y_base,\n            side=\"positive\",\n            width=cloud_width,\n            line_color=color,\n            fillcolor=color,\n            opacity=0.55,\n            meanline_visible=False,\n            box_visible=False,\n            points=False,\n            name=condition,\n            legendgroup=condition,\n            showlegend=False,\n            hoveron=\"violins\",\n            hoverinfo=\"x+name\",\n            orientation=\"h\",\n            scalemode=\"width\",\n        )\n    )\n\n    # Box plot — centered on the category baseline, outline tinted with category color\n    fig.add_trace(\n        go.Box(\n            x=values,\n            y=y_base,\n            width=box_width,\n            marker_color=color,\n            line={\"color\": color, \"width\": 1.6},\n            fillcolor=ELEVATED_BG,\n            boxpoints=False,\n            name=condition,\n            legendgroup=condition,\n            showlegend=False,\n            orientation=\"h\",\n            hoverinfo=\"skip\",\n        )\n    )\n\n    # Rain — idiomatic scatter falling downward below the baseline\n    rng = np.random.default_rng(42 + i)\n    y_jitter = rng.uniform(-rain_jitter_amp, rain_jitter_amp, size=len(values))\n    fig.add_trace(\n        go.Scatter(\n            x=values,\n            y=i + rain_offset + y_jitter,\n            mode=\"markers\",\n            marker={\"size\": 6, \"color\": color, \"opacity\": 0.6, \"line\": {\"width\": 0.5, \"color\": PAGE_BG}},\n            name=condition,\n            legendgroup=condition,\n            showlegend=False,\n            hovertemplate=f\"<b>{condition}</b><br>%{{x:.0f}} ms<extra></extra>\",\n        )\n    )\n\n# Title — length-scaled fontsize (baseline 16px @ 67 chars)\ntitle = \"raincloud-basic · python · plotly · anyplot.ai\"\ntitle_fontsize = max(11, round(16 * 67 / len(title))) if len(title) > 67 else 16\n\n# Layout\nfig.update_layout(\n    autosize=False,\n    width=800,\n    height=450,\n    margin={\"l\": 110, \"r\": 40, \"t\": 60, \"b\": 60},\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    title={\n        \"text\": title,\n        \"font\": {\"size\": title_fontsize, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n        \"y\": 0.96,\n        \"yanchor\": \"top\",\n    },\n    font={\"color\": INK},\n    yaxis={\n        \"title\": {\"text\": \"Experimental Condition\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"tickfont\": {\"size\": 11, \"color\": INK_SOFT},\n        \"tickmode\": \"array\",\n        \"tickvals\": list(range(len(conditions))),\n        \"ticktext\": conditions,\n        \"range\": [-0.45, len(conditions) - 0.45],\n        \"showgrid\": False,\n        \"zeroline\": False,\n        \"showline\": False,\n        \"ticks\": \"\",\n    },\n    xaxis={\n        \"title\": {\"text\": \"Reaction Time (ms)\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"tickfont\": {\"size\": 11, \"color\": INK_SOFT},\n        \"gridcolor\": GRID,\n        \"gridwidth\": 1,\n        \"range\": x_range,\n        \"zeroline\": False,\n        \"showline\": True,\n        \"linecolor\": INK_SOFT,\n        \"linewidth\": 1,\n    },\n    violingap=0,\n    violinmode=\"overlay\",\n    showlegend=False,\n    hoverlabel={\"bgcolor\": ELEVATED_BG, \"bordercolor\": INK, \"font\": {\"size\": 11, \"color\": INK}},\n)\n\n# Save PNG (static) at the canonical landscape canvas: 3200×1800\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\n\n# Save HTML with range slider for interactive exploration\nfig.update_xaxes(rangeslider={\"visible\": True, \"thickness\": 0.05})\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}