13 · Plotting¶
You will learn how to draw a study's values with study.plot: distributions per group, pooled or one
value per subject, curves with their intervals, cumulative distributions, agreement between methods, and how
to style figures so a group keeps its colour everywhere. Then how to draw results: tests with their brackets,
forest plots of estimates, normality checks.
You need 04 · Selecting and reshaping and 10 · Tests and corrections.
import matplotlib.pyplot as plt
import synaptome as syn
Pooled values, or one value per subject¶
In the method comparison, error has one value per subject, method, sensor type and source. A violin of the
EEG errors per method pools every source of every subject, and its title says so.
mc = syn.datasets.method_comparison()
fig, ax = mc.plot.violin("error", x="method", subset={"sensors": "eeg"})
Pooling is fine to look at a distribution, but sources of one subject aren't independent. unit="subject"
first collapses each subject's sources to one value (their mean, or unit_reduce="median"): one point per
subject, the units a test would compare.
p = mc.plot.box("error", x="method", subset={"sensors": "eeg"}, unit="subject", unit_reduce="median")
Every view returns a Plot: it unpacks as fig, ax and keeps the numbers it drew, so a figure and a
table never disagree.
p.data[["method", "n", "median", "q1", "q3"]]
| method | n | median | q1 | q3 | |
|---|---|---|---|---|---|
| 0 | alternative | 11 | 0.297308 | 0.289982 | 0.314670 |
| 1 | baseline | 16 | 0.249244 | 0.241701 | 0.262697 |
| 2 | proposed | 16 | 0.197275 | 0.189129 | 0.209184 |
Centers and intervals¶
center and interval choose the summary. interval="ci" is the same confidence interval
q.estimate(...) reports. A hue adds a second grouping.
p = mc.plot.point("error", x="sensors", hue="method", unit="subject", center="mean", interval="ci")
Along an ordered dimension, line draws the center with its interval as a band:
weights = syn.datasets.longitudinal()
p = weights.plot.line("weight", x="week", hue="group", unit="subject")
Cumulative distributions¶
An ECDF shows, for every error value, the share of sources at or below it: where one curve lies left of
another, that method's errors are smaller. With per_unit="subject", each subject is a faint curve and the
bold one is their median, so every subject counts the same whatever its number of sources.
p = mc.plot.ecdf("error", hue="method", subset={"sensors": "eeg"}, per_unit="subject")
Agreement between methods¶
A Bland–Altman plot compares each method with a reference, subject by subject: the mean of the two values horizontally, their difference vertically, with the bias (mean difference) and the 95% limits of agreement.
p = mc.plot.agreement("error", by="method", reference="baseline", subset={"sensors": "eeg"}, unit="subject")
p.data
| competitor | reference | n | bias | sd | low | high | |
|---|---|---|---|---|---|---|---|
| 0 | alternative | baseline | 11 | 0.050706 | 0.009231 | 0.032613 | 0.068799 |
| 1 | proposed | baseline | 16 | -0.051661 | 0.013932 | -0.078969 | -0.024354 |
A scatter of one method against another, one point per subject, with the identity line: points below it
are subjects where proposed has the smaller error.
p = mc.plot.scatter(
"error",
x="method",
pair=("baseline", "proposed"),
identity=True,
subset={"sensors": "eeg"},
unit="subject",
)
Style and colours¶
A group's colour depends on its place among all the groups of the study, not on what is drawn, so
proposed is the same colour in every figure, even when alternative is left out. set_style (or the style
context manager) sets one font size and palette for every figure; ax= draws into your own layout.
fig, axes = plt.subplots(1, 2, figsize=(9, 3.5))
with syn.plot.style(fontsize=9):
mc.plot.box("error", x="method", subset={"sensors": "mag"}, unit="subject", ax=axes[0], title="MAG")
mc.plot.box(
"error",
x="method",
subset={"sensors": "grad", "method": ["baseline", "proposed"]},
unit="subject",
ax=axes[1],
title="GRAD",
)
fig.tight_layout()
Figures of results¶
A result draws exactly what its test compared. plot() picks the usual figure: for a test, the groups with a
bracket on each significant pair (after correction), and, in a paired design, each subject's values joined.
q = mc.compare(
"error",
by="method",
reference="proposed",
subset={"sensors": "eeg"},
reduce={"source": "median"},
higher_is_better=False,
title="EEG",
)
r = q.test("wilcoxon", alternative="less")
p = r.plot(annotate=("stars", "p", "effect"))
The other views are on study.plot: differences (what a paired test compared), estimate (a forest
of several results), along (curves and their clusters), normality and omnibus.
p = mc.plot.differences(r)
results = syn.correct(
[
mc.compare(
"error",
by="method",
reference="proposed",
subset={"sensors": s},
reduce={"source": "median"},
higher_is_better=False,
title=s.upper(),
).test("wilcoxon", alternative="less")
for s in ["eeg", "mag", "grad"]
]
)
p = mc.plot.estimate(results, effect_size="hodges_lehmann")
p = q.test("shapiro").plot(kind="qq")
p = q.test("friedman").plot()
A result is drawn only on the data it was computed on. Change the values it read (here, every EEG error of one subject with the proposed method), and its figure is refused until the question is asked again:
changed = mc.copy()
s01 = changed.to_df("error").query("subject == 's01' and method == 'proposed'")
changed.add("error", s01.assign(error=s01["error"] + 0.1), overwrite=True)
try:
changed.plot.comparison(r)
except ValueError as err:
print(err)
This result is out of date: the data its question reads changed since it was computed (2026-11-27 08:23 UTC), so a figure would show other data than the one tested. Ask the question again (study.question(result.question_spec)) and recompute the result.
Next: this is the last tutorial for now. The API reference documents every argument.