Palmer Penguins¶

In [1]:
import logging
from datetime import UTC, datetime

from palmerpenguins import load_penguins
from plotnine import (
    aes,
    facet_wrap,
    geom_boxplot,
    geom_jitter,
    geom_point,
    geom_smooth,
    ggplot,
    labs,
    scale_color_manual,
    scale_fill_manual,
    theme,
    theme_minimal,
)

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s", force=True
)
logger = logging.getLogger("jupyter-hello")

raw = load_penguins()
penguins = raw.dropna()
logger.info(
    "Loaded %d penguins, dropped %d with missing values",
    len(penguins),
    len(raw) - len(penguins),
)
palette = {"Adelie": "#FF8C00", "Chinstrap": "#A034F0", "Gentoo": "#159090"}

print(f"Rendered at {datetime.now(UTC):%Y-%m-%d %H:%M:%S} UTC")
2026-10-08 21:20:46,264 INFO Loaded 333 penguins, dropped 11 with missing values
Rendered at 2026-10-09 04:20:46 UTC

Species summary¶

In [2]:
logger.info("Summarizing %d species", penguins["species"].nunique())
(
    penguins.groupby("species")
    .agg(
        count=("species", "size"),
        bill_length_mm=("bill_length_mm", "mean"),
        bill_depth_mm=("bill_depth_mm", "mean"),
        flipper_length_mm=("flipper_length_mm", "mean"),
        body_mass_g=("body_mass_g", "mean"),
    )
    .round(1)
)
2026-10-08 21:20:46,267 INFO Summarizing 3 species
Out[2]:
count bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
species
Adelie 146 38.8 18.3 190.1 3706.2
Chinstrap 68 48.8 18.4 195.8 3733.1
Gentoo 119 47.6 15.0 217.2 5092.4

Bill length vs. depth¶

In [3]:
logger.info("Plotting bill length vs. depth")
(
    ggplot(penguins, aes("bill_length_mm", "bill_depth_mm"))
    + geom_smooth(method="lm", color="#444444", linetype="dashed", se=False)
    + geom_point(aes(color="species"), alpha=0.6, size=2)
    + geom_smooth(aes(color="species"), method="lm", se=False, size=1.2)
    + scale_color_manual(values=palette)
    + labs(x="Bill length (mm)", y="Bill depth (mm)", color="Species")
    + theme_minimal()
    + theme(figure_size=(8, 5))
)
2026-10-08 21:20:46,277 INFO Plotting bill length vs. depth
Out[3]:
No description has been provided for this image

Body mass by species and sex¶

In [4]:
logger.info("Plotting body mass by species and sex")
(
    ggplot(penguins, aes("sex", "body_mass_g", fill="species"))
    + geom_boxplot(alpha=0.4, outlier_shape="", width=0.6)
    + geom_jitter(aes(color="species"), width=0.15, alpha=0.6, size=1.5)
    + facet_wrap("species")
    + scale_fill_manual(values=palette)
    + scale_color_manual(values=palette)
    + labs(x="", y="Body mass (g)")
    + theme_minimal()
    + theme(figure_size=(9, 4), legend_position="none")
)
2026-10-08 21:21:08,578 INFO Plotting body mass by species and sex
Out[4]:
No description has been provided for this image