SparklinePlot¶
Line/sparkline plot for visualizing a numeric field as a function of another field.
Class: diffract.viz.plots.sparkline.SparklinePlot (also
diffract.viz.plots.SparklinePlot, and the alias Sparkline).
Basic usage¶
from diffract.viz.data import FieldRef
from diffract.viz.plots.sparkline import SparklinePlot
with session:
plot = SparklinePlot(y=FieldRef("stable_rank"), x=FieldRef("layer_id"))
fig = session.viz.draw(plot=plot)
Or using the convenience method (aliased as session.viz.line):
with session:
fig = session.viz.sparkline(y="stable_rank", x="layer_id")
Example: Compare models across layers¶
plot = SparklinePlot(
y=FieldRef("frob_norm"),
x=FieldRef("layer_id"),
group_by=FieldRef("model_id"),
line_color=FieldRef("model_id"),
title="Frobenius Norm by Layer",
)
Example: With error bands¶
When multiple entries share the same (group_by, x) pair, the plot shows
mean +/- std as a shaded band (show_bands=True by default):
# If multiple heads per layer, this shows mean across heads with a std band
plot = SparklinePlot(
y=FieldRef("stable_rank"),
x=FieldRef("layer_id"),
group_by=FieldRef("model_id"),
)
Example: Different line styles¶
plot = SparklinePlot(
y=FieldRef("stable_rank"),
x=FieldRef("layer_id"),
group_by=FieldRef("model_id"),
line_color=FieldRef("model_id"),
line_dash=FieldRef("kind"), # different dash patterns per kind
marker_symbol=FieldRef("kind"), # different markers per kind
)
Parameters¶
Fields (required, keyword-only)¶
Parameter |
Type |
Description |
|---|---|---|
|
|
Field for Y-axis values |
|
|
Field for X-axis values |
Grouping¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Field to split into separate line traces |
Rescaling¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Rescale x-values into this range |
|
|
|
Rescale y-values into this range |
|
|
|
Rescale x per trace |
|
|
|
Rescale y per trace |
Titles¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Figure title (defaults to |
|
|
|
X-axis title |
|
|
|
Y-axis title |
Mode and bands¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Plotly draw mode |
|
|
|
Draw mean +/- std bands |
|
|
|
Band fill opacity |
|
|
|
Band edge line width |
Line styling (from SupportsLine)¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Line color (constant or by field) |
|
|
|
Line dash pattern |
|
|
|
Line width |
Marker styling (from SupportsMarker)¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Marker color |
|
|
|
Marker symbol |
|
|
|
Marker size |
|
|
|
Marker opacity |
Value filtering¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Filter conditions |
Theming¶
Pass a Theme at render time (not a constructor field):
from diffract.viz.styling import DARK_THEME
fig = session.viz.draw(plot=plot, theme=DARK_THEME)
YAML configuration¶
plot:
_target_: diffract.viz.plots.sparkline.SparklinePlot
y: stable_rank
x: layer_id
group_by: model_id
line_color: model_id
mode: "lines+markers"
title: "Stable Rank by Layer"
How it works¶
Fetches data via
DataProvider.fetch([...], ...).Groups entries by the
group_byfield (one line per group).Within each group, computes mean and std per unique x value.
Orders x values by the
xFieldRefordering.Creates a
go.Scattertrace for the mean line.Adds fill-between band traces for mean +/- std when
show_bands=Trueand any std is nonzero.Applies line/marker mappings and the theme when one is passed to
render.
Aggregation within groups¶
When multiple entries share the same (group_by, x) combination:
Model A:
layer_id=0: [head0=45.2, head1=43.1, head2=47.3] -> mean=45.2, std=2.1
layer_id=1: [head0=38.5, head1=40.2, head2=39.1] -> mean=39.3, std=0.8
Result: Line with mean values, shaded band showing +/-1 std
Mode options¶
Mode |
Description |
|---|---|
|
Lines only (default) |
|
Points only |
|
Lines with points |
If marker_symbol is set and mode does not include markers, markers are
added automatically.
Dash patterns¶
Available dash patterns (used by line_dash as a constant, or supplied by the
theme’s dash palette when mapping from a field):
"solid"— Solid line"dot"— Dotted"dash"— Dashed"dashdot"— Dash-dot"longdash"— Long dashes"longdashdot"— Long dash-dot
Common use cases¶
Layer-wise analysis¶
plot = SparklinePlot(
y=FieldRef("stable_rank"),
x=FieldRef("layer_id"),
group_by=FieldRef("kind"), # separate lines for attn, ffn
line_color=FieldRef("kind"),
)
Cross-model comparison¶
plot = SparklinePlot(
y=FieldRef("effective_rank"),
x=FieldRef("layer_id"),
group_by=FieldRef("model_id"),
line_color=FieldRef("model_id"),
line_dash=FieldRef("model_id"),
)
Notes¶
X values are ordered according to the
xFieldRefordering before plotting.Error bands use fill-between with reduced opacity (
band_opacity).The legend shows one entry per group (not per band).
See also¶
ScatterPlot — For X/Y scatter of two numeric fields
BoxPlot — For distributions at each X value