Grouped Box Plot — lets-plot

A grouped box plot displays multiple box plots side-by-side within each category, enabling comparison of distributions across subgroups. Each group contains boxes representing different subcategories or conditions, making it ideal for multi-factor comparisons and A/B testing scenarios with multiple metrics.

Grouped Box Plot rendered with lets-plot

Renders

Python source (lets-plot)

""" anyplot.ai
box-grouped: Grouped Box Plot
Library: letsplot 4.11.0 | Python 3.13.15
Quality: 87/100 | Updated: 2026-08-18
"""

import os
import shutil

import numpy as np
import pandas as pd
from lets_plot import (
    LetsPlot,
    aes,
    element_blank,
    element_line,
    element_rect,
    element_text,
    geom_boxplot,
    geom_point,
    ggplot,
    ggsave,
    ggsize,
    labs,
    position_dodge,
    scale_fill_manual,
    theme,
    theme_minimal,
)


LetsPlot.setup_html()

# Theme tokens (Imprint palette)
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"

IMPRINT = ["#009E73", "#C475FD", "#4467A3"]

# Data: Employee performance scores by department and experience level
np.random.seed(42)

departments = ["Engineering", "Marketing", "Sales", "Operations"]
experience_levels = ["Junior", "Mid-Level", "Senior"]

data = []
for dept in departments:
    for exp in experience_levels:
        n = 40
        if exp == "Junior":
            base = 50 + np.random.choice([0, 5, 10])
            spread = 12
        elif exp == "Mid-Level":
            base = 60 + np.random.choice([0, 3, 6])
            spread = 10
        else:
            base = 72 + np.random.choice([0, 2, 4])
            spread = 8

        dept_offset = {"Engineering": 3, "Marketing": 0, "Sales": -2, "Operations": 1}[dept]
        values = np.random.normal(base + dept_offset, spread, n)

        if np.random.random() > 0.5:
            outlier_low = base - 3 * spread + np.random.randn(2) * 2
            outlier_high = base + 3 * spread + np.random.randn(2) * 2
            values = np.concatenate([values, outlier_low, outlier_high])

        for v in values:
            data.append({"Department": dept, "Experience": exp, "Performance Score": v})

df = pd.DataFrame(data)
df["Experience"] = pd.Categorical(df["Experience"], categories=experience_levels, ordered=True)
df["Department"] = pd.Categorical(df["Department"], categories=departments, ordered=True)

# Per-group means overlaid as diamonds so the box (median/IQR) and the mean
# read as two distinct summaries instead of one flat shape per group.
means = df.groupby(["Department", "Experience"], observed=True)["Performance Score"].mean().reset_index()

# Plot
dodge = position_dodge(width=0.7)
plot = (
    ggplot(df, aes(x="Department", y="Performance Score", fill="Experience"))
    + geom_boxplot(alpha=0.85, outlier_size=1.3, outlier_alpha=0.6, width=0.7, size=0.6, position=dodge)
    + geom_point(
        aes(x="Department", y="Performance Score", group="Experience"),
        data=means,
        position=dodge,
        shape=18,
        size=3,
        color=ELEVATED_BG,
        inherit_aes=False,
        show_legend=False,
    )
    + scale_fill_manual(values=IMPRINT)
    + labs(
        title="box-grouped · python · letsplot · anyplot.ai",
        x="Department",
        y="Performance Score",
        fill="Experience Level",
    )
    + theme_minimal()
    + theme(
        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),
        panel_background=element_rect(fill=PAGE_BG),
        panel_grid_major_x=element_blank(),
        panel_grid_major_y=element_line(color=INK_SOFT, size=0.3),
        panel_grid_minor_x=element_blank(),
        panel_grid_minor_y=element_blank(),
        plot_title=element_text(size=16, face="bold", color=INK),
        axis_title=element_text(size=12, color=INK),
        axis_text=element_text(size=10, color=INK_SOFT),
        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),
        legend_title=element_text(size=11, color=INK),
        legend_text=element_text(size=10, color=INK_SOFT),
        legend_position="right",
    )
    + ggsize(800, 450)
)

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

# Move files from lets-plot-images to current directory
shutil.move(f"lets-plot-images/plot-{THEME}.png", f"plot-{THEME}.png")
shutil.move(f"lets-plot-images/plot-{THEME}.html", f"plot-{THEME}.html")

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/box-grouped/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": "box-grouped",
  "language": "python",
  "library": "letsplot",
  "page": "https://anyplot.ai/box-grouped/python/letsplot",
  "hub": "https://anyplot.ai/box-grouped",
  "code_json": "https://api.anyplot.ai/specs/box-grouped/letsplot/code",
  "spec_json": "https://api.anyplot.ai/specs/box-grouped",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/box-grouped/python/letsplot/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/box-grouped/python/letsplot/plot-dark.png",
  "interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/box-grouped/python/letsplot/plot-light.html",
  "interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/box-grouped/python/letsplot/plot-dark.html",
  "quality_score": 87.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Grouped Box Plot on anyplot.ai.

Other implementations