Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 74 additions & 54 deletions plots/raincloud-basic/implementations/python/letsplot.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,62 @@
""" pyplots.ai
""" anyplot.ai
raincloud-basic: Basic Raincloud Plot
Library: letsplot 4.8.2 | Python 3.14
Quality: 94/100 | Created: 2025-12-25
Library: letsplot 4.10.1 | Python 3.13.13
Quality: 94/100 | Updated: 2026-05-26
"""

import os

import numpy as np
import pandas as pd
from lets_plot import *
from lets_plot.export import ggsave
from lets_plot import (
LetsPlot,
aes,
arrow,
element_blank,
element_line,
element_rect,
element_text,
geom_boxplot,
geom_jitter,
geom_segment,
geom_text,
geom_violin,
ggplot,
ggsave,
ggsize,
labs,
position_nudge,
scale_color_manual,
scale_fill_manual,
scale_x_continuous,
scale_y_discrete,
theme,
)


LetsPlot.setup_html()

# Data - Reaction times (ms) for three experimental conditions
# Theme tokens
THEME = os.getenv("ANYPLOT_THEME", "light")
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"
RULE = "rgba(26,26,23,0.15)" if THEME == "light" else "rgba(240,239,232,0.15)"

# anyplot categorical palette (canonical order, first series ALWAYS #009E73)
BRAND = "#009E73"
LAVENDER = "#C475FD"
BLUE = "#4467A3"

# Data — reaction times (ms) for three experimental conditions
np.random.seed(42)

# Control group: normal distribution centered at 450ms
control = np.random.normal(450, 60, 80)

# Treatment A: faster responses, centered at 380ms
treatment_a = np.random.normal(380, 50, 80)

# Treatment B: bimodal distribution — two widely separated response clusters
treatment_b = np.concatenate([np.random.normal(300, 30, 50), np.random.normal(540, 35, 30)])

# Build dataframe
df = pd.DataFrame(
{
"condition": ["Control"] * len(control)
Expand All @@ -34,84 +66,72 @@
}
)

# Color palette (mapped to display order: Treatment B, Treatment A, Control)
palette = {"Control": "#306998", "Treatment A": "#FFD43B", "Treatment B": "#5BA85B"}
palette = {"Control": BRAND, "Treatment A": LAVENDER, "Treatment B": BLUE}

# Category display order (bottom to top): Treatment B, Treatment A, Control
# Display order (bottom to top of y-axis): Treatment B, Treatment A, Control
cat_order = ["Treatment B", "Treatment A", "Control"]

# Create raincloud plot: half-violin (cloud) + box plot + jittered points (rain)
# Y-axis: Treatment B=0, Treatment A=1, Control=2
plot = (
ggplot(df, aes(x="reaction_time", y="condition", fill="condition", color="condition"))
# Half-violin (cloud) - nudged above category baseline
# Half-violin (cloud) nudged above the category baseline
+ geom_violin(trim=False, show_half=1, size=0.8, alpha=0.7, position=position_nudge(y=0.12))
# Box plot - wider for clear quartile readability
# Boxplot on the baseline with elevated-bg fill for contrast in both themes
+ geom_boxplot(
width=0.18,
outlier_size=0,
outlier_alpha=0,
fill="white",
color="#333333",
fill=ELEVATED_BG,
color=INK,
size=0.8,
alpha=0.95,
show_legend=False,
)
# Jittered points (rain) - positioned below category baseline
# Rain — jittered points below the baseline
+ geom_jitter(
width=0,
height=0.06,
size=3.5,
alpha=0.6,
height=0.05,
size=4.0,
alpha=0.5,
shape=21,
stroke=0.3,
show_legend=False,
position=position_nudge(y=-0.16),
)
# Annotation: Treatment A faster responses (Treatment A = index 1)
# Annotation: Treatment A faster mean — text on the right (off the cloud), arrow lands just above the peak top
+ geom_text(
x=580, y=1.55, label="~70ms faster mean\nthan Control", size=10, color="#8B7500", fontface="italic", hjust=0
x=690, y=1.55, label="~70ms faster mean\nthan Control", size=13, color=INK_SOFT, fontface="italic", hjust=1
)
# Arrow from label toward Treatment A
+ geom_segment(x=570, y=1.45, xend=420, yend=1.15, color="#8B7500", size=0.5, arrow=arrow(length=8, type="closed"))
# Annotation: Treatment B bimodal (Treatment B = index 0, bottom)
+ geom_segment(x=550, y=1.45, xend=400, yend=1.70, color=INK_SOFT, size=0.5, arrow=arrow(length=8, type="closed"))
# Annotation: Treatment B bimodal — text on the right, arrows land just above each peak top
+ geom_text(
x=580, y=0.55, label="Two distinct\nresponse clusters", size=10, color="#3D7A3D", fontface="italic", hjust=0
x=690, y=0.55, label="Two distinct\nresponse clusters", size=13, color=INK_SOFT, fontface="italic", hjust=1
)
# Arrow pointing to Treatment B fast cluster (~300ms)
+ geom_segment(x=570, y=0.35, xend=310, yend=0.12, color="#3D7A3D", size=0.5, arrow=arrow(length=8, type="closed"))
# Arrow pointing to Treatment B slow cluster (~540ms)
+ geom_segment(x=585, y=0.3, xend=545, yend=0.12, color="#3D7A3D", size=0.5, arrow=arrow(length=8, type="closed"))
# Scales
+ geom_segment(x=550, y=0.45, xend=310, yend=0.55, color=INK_SOFT, size=0.5, arrow=arrow(length=8, type="closed"))
+ geom_segment(x=620, y=0.30, xend=555, yend=0.40, color=INK_SOFT, size=0.5, arrow=arrow(length=8, type="closed"))
+ scale_fill_manual(values=palette)
+ scale_color_manual(values=palette)
+ scale_y_discrete(limits=cat_order)
+ scale_x_continuous(limits=[200, 700])
# Labels and title
+ labs(
x="Reaction Time (ms)", y="Experimental Condition", title="raincloud-basic \u00b7 letsplot \u00b7 pyplots.ai"
)
# Refined theme — custom styling without flavor to avoid conflicts
+ labs(x="Reaction Time (ms)", y="Experimental Condition", title="raincloud-basic · python · letsplot · anyplot.ai")
+ theme(
plot_title=element_text(size=24, face="bold"),
plot_background=element_rect(fill="#FAFAFA", color="#FAFAFA"),
panel_background=element_rect(fill="#FAFAFA", color="#FAFAFA"),
axis_title_x=element_text(size=20, margin=[12, 0, 0, 0]),
axis_title_y=element_text(size=20, margin=[0, 12, 0, 0]),
axis_text_x=element_text(size=16),
axis_text_y=element_text(size=16, face="bold"),
plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
plot_title=element_text(size=16, face="bold", color=INK),
axis_title_x=element_text(size=12, color=INK, margin=[12, 0, 0, 0]),
axis_title_y=element_text(size=12, color=INK, margin=[0, 12, 0, 0]),
axis_text_x=element_text(size=10, color=INK_SOFT),
axis_text_y=element_text(size=12, color=INK, face="bold"),
axis_ticks=element_blank(),
axis_line_x=element_line(color="#CCCCCC", size=0.5),
axis_line_x=element_blank(),
axis_line_y=element_blank(),
legend_position="none",
panel_grid_major_y=element_blank(),
panel_grid_minor=element_blank(),
panel_grid_major_x=element_line(color="rgba(0, 0, 0, 0.08)", size=0.4),
panel_grid_major_x=element_line(color=RULE, size=0.4),
plot_margin=[40, 40, 30, 20],
)
+ ggsize(1600, 900)
+ ggsize(800, 450)
)

# Save outputs
ggsave(plot, "plot.png", path=".", scale=3)
ggsave(plot, "plot.html", path=".")
ggsave(plot, f"plot-{THEME}.png", path=".", scale=4)
ggsave(plot, f"plot-{THEME}.html", path=".")
Loading
Loading