Skip to content
64 changes: 46 additions & 18 deletions plots/qq-basic/implementations/python/seaborn.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
""" anyplot.ai
qq-basic: Basic Q-Q Plot
Library: seaborn 0.13.2 | Python 3.14.4
Quality: 88/100 | Updated: 2026-04-27
Library: seaborn 0.13.2 | Python 3.13.14
Quality: 92/100 | Updated: 2026-07-24
"""

import os

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns


Expand All @@ -17,7 +18,7 @@
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
BRAND = "#009E73" # Okabe-Ito position 1
BRAND = "#009E73" # Imprint palette position 1

sns.set_theme(
style="ticks",
Expand All @@ -37,8 +38,8 @@
)

# Data - mixture distribution with right-skewed tail to demonstrate Q-Q deviation
np.random.seed(42)
sample = np.concatenate([np.random.normal(loc=50, scale=10, size=180), np.random.normal(loc=75, scale=5, size=20)])
rng = np.random.default_rng(42)
sample = np.concatenate([rng.normal(loc=50, scale=10, size=180), rng.normal(loc=75, scale=5, size=20)])
n = len(sample)

# Theoretical quantiles via Abramowitz & Stegun 26.2.17 rational approximation
Expand All @@ -49,30 +50,57 @@
theoretical_q = np.where(p < 0.5, -(t - num / den), t - num / den)
sample_q = np.sort((sample - sample.mean()) / sample.std(ddof=1))

# Simulation envelope: draw many perfectly-normal replicates of size n and let
# seaborn's lineplot bootstrap a 95% percentile interval around their order
# statistics. This gives the reference guide real statistical depth (the
# natural sampling variability a truly normal sample would show) instead of a
# bare y=x line, and leverages seaborn's own error-bar estimation.
n_replicates = 300
replicates = np.sort(rng.standard_normal(size=(n_replicates, n)), axis=1)
envelope_df = pd.DataFrame({"theoretical": np.tile(theoretical_q, n_replicates), "simulated": replicates.ravel()})

# Plot
fig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)
fig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)
ax.set_facecolor(PAGE_BG)

sns.scatterplot(x=theoretical_q, y=sample_q, ax=ax, s=200, color=BRAND, alpha=0.7, edgecolor=PAGE_BG, linewidth=0.5)
sns.lineplot(
data=envelope_df,
x="theoretical",
y="simulated",
ax=ax,
estimator="mean",
errorbar=("pi", 95),
color=INK_SOFT,
linewidth=1.5,
linestyle="--",
label="Normal reference (95% envelope)",
zorder=1,
)

# Reference line (y=x for perfect normal distribution)
q_min = min(theoretical_q.min(), sample_q.min())
q_max = max(theoretical_q.max(), sample_q.max())
ax.plot(
[q_min, q_max], [q_min, q_max], color=INK_SOFT, linewidth=2.5, linestyle="--", label="Reference (y=x)", zorder=1
sns.scatterplot(
x=theoretical_q,
y=sample_q,
ax=ax,
s=90,
color=BRAND,
alpha=0.75,
edgecolor=PAGE_BG,
linewidth=0.8,
label="Sample quantiles",
zorder=2,
)

# Style
ax.set_xlabel("Theoretical Quantiles", fontsize=20, color=INK)
ax.set_ylabel("Sample Quantiles", fontsize=20, color=INK)
ax.set_title("qq-basic · seaborn · anyplot.ai", fontsize=24, fontweight="medium", color=INK)
ax.tick_params(axis="both", labelsize=16, colors=INK_SOFT)
ax.legend(fontsize=16, loc="upper left")
ax.set_xlabel("Theoretical Quantiles", fontsize=10, color=INK)
ax.set_ylabel("Sample Quantiles", fontsize=10, color=INK)
ax.set_title("qq-basic · python · seaborn · anyplot.ai", fontsize=12, fontweight="medium", color=INK)
ax.tick_params(axis="both", labelsize=8, colors=INK_SOFT)
ax.legend(fontsize=8, loc="upper left")
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
for s in ("left", "bottom"):
ax.spines[s].set_color(INK_SOFT)
ax.grid(True, alpha=0.10, linewidth=0.8, color=INK)

plt.tight_layout()
plt.savefig(f"plot-{THEME}.png", dpi=300, bbox_inches="tight", facecolor=PAGE_BG)
plt.savefig(f"plot-{THEME}.png", dpi=400, facecolor=PAGE_BG)
Loading
Loading