Skip to content

stack ​

Stacks groups edge-to-edge along an axis with no gap between them — spread without the spacing. The basis for stacked bar charts and pie charts.

python
from gofish import chart, spread, stack, rect

chart(seafood, axes=True).flow(
    spread(by="lake", dir="x"),
    stack(by="species", dir="y"),
).mark(rect(h="count", fill="species")).render(w=500, h=300)

Signature ​

python
stack(children=None, *, by=None, dir, **options) -> Operator | Mark

Like spread, stack is polymorphic: called with no positional argument it returns an operator for use inside .flow(); called with a positional list of marks it returns a combinator-form mark that stacks those explicit children (the low-level form behind the stackX/stackY operators).

Parameters ​

stack — Operator form ​

spread({ glue: true }) under its own wire tag — children glued together (touching, no gaps).

OptionTypeDefaultDescription
bystr | AnyField to partition rows by; also accepts a field(...) accessor carrying domain ops (sort/reverse/bin).
dirstrAxis to stack along: x, y, or an axis name the enclosing coordinate space declares (polar theta/r, geo lon/lat).
spacingfloatForwarded to the underlying spread. Glue semantics force the effective gap to 0; accepted for spread-parity.
glueboolSpread-parity passthrough; stack always glues regardless.
alignmentstr"baseline"Cross-axis alignment ("start" | "middle" | "end" | "baseline").
shared_scaleboolFalseShare one scale across all children.
anchorstr"edge"Whether spacing is measured between facing edges (edge), or as a fixed pitch between the named anchor point on each child.
reverseboolFalseReverse the children's order along dir.
axesAny
xint | float | strLeft edge of this operator's box, in the parent's space (pixels). Omitted, the parent places it.
yint | float | strStart edge on y (top where y reads top-down, bottom where it grows upward) of this operator's box, in the parent's space (pixels). Omitted, the parent places it.
wint | float | strData-driven cross-axis extent (field/datum-sized children).
hint | float | strData-driven cross-axis extent (field/datum-sized children).
sizeint | float | strPer-entry stack-axis extent (field/datum-sized children); a field(...).normalize() accessor makes it a space-filling spine.

stack — Combinator form ​

Low-level combinator form of stack. Same fields as the operator form (OPERATORS.stack) plus key and the full box-dims group.

OptionTypeDefaultDescription
xint | float | strLeft edge of this operator's box, in the parent's space (pixels). Omitted, the parent places it.
wint | float | strData-driven cross-axis extent (field/datum-sized children).
yint | float | strStart edge on y (top where y reads top-down, bottom where it grows upward) of this operator's box, in the parent's space (pixels). Omitted, the parent places it.
hint | float | strData-driven cross-axis extent (field/datum-sized children).
bystr | AnyField to partition rows by; also accepts a field(...) accessor carrying domain ops (sort/reverse/bin).
dirstrAxis to stack along: x, y, or an axis name the enclosing coordinate space declares (polar theta/r, geo lon/lat).
spacingfloatForwarded to the underlying spread. Glue semantics force the effective gap to 0; accepted for spread-parity.
glueboolSpread-parity passthrough; stack always glues regardless.
alignmentstr"baseline"Cross-axis alignment ("start" | "middle" | "end" | "baseline").
shared_scaleboolFalseShare one scale across all children.
anchorstr"edge"Whether spacing is measured between facing edges (edge), or as a fixed pitch between the named anchor point on each child.
reverseboolFalseReverse the children's order along dir.
axesAny
sizeint | float | strPer-entry stack-axis extent (field/datum-sized children); a field(...).normalize() accessor makes it a space-filling spine.
keystrInternal per-node key override.

Box dimensions ​

OptionTypeDefaultDescription
cxint | float | strCenter x.
x2int | float | strRight edge position.
em_xboolEmbed x in the parent's x space.
cyint | float | strCenter y.
y2int | float | strOther y edge position.
em_yboolEmbed y in the parent's y space.
dimsdictBox dimensions by axis name: x/y, or a name the enclosing coordinate space declares (polar theta/r, geo lon/lat). Each value is a position (like x) or an interval {min, center, max, size, embedded}.

Returns an Operator for use inside .flow().

dir takes "x", "y", or an axis name the enclosing coordinate space declares, such as "theta" under polar; see spread → naming the axis.

Signed parts ​

stack places its parts end to end, in order. Each part starts where the one before it ends, and the first part starts at 0. A negative part goes back, so the stack ends at the sum of its parts, and parts that cancel overlap. Parts (+100, −30, +20, −50, +10) rise to 100, step back and forth, and end at 50.

To pile positive parts up from 0 and negative parts down from 0 (a diverging stacked bar), group by sign first, so each stack holds one sign:

python
# `direction` is "Inflow" for positive amounts and "Outflow" for negative.
chart(cash_flows).flow(
    spread(by="quarter", dir="x"),
    group(by="direction"),
    stack(by="flow", dir="y"),
).mark(rect(h="amount", fill="flow"))

Centered stacks ​

When the chart's schema gives the by column a midpoint (Schema.ordered(levels).diverging()), the stack's 0 is the midpoint of that order instead of the start of its first part. The parts must be nonnegative. This draws Likert charts and population pyramids:

python
chart(
    survey, schema={"response": Schema.ordered(LEVELS).diverging()}
).flow(
    spread(by="question", dir="y"),
    stack(by="response", dir="x"),
).mark(rect(w="count", fill="response"))

Examples ​

python
# Stacked bars: lakes across x, species stacked up y
chart(seafood).flow(
    spread(by="lake", dir="x"),
    stack(by="species", dir="y"),
).mark(rect(h="count", fill="species"))

# Grouped bars: stack along x instead
chart(seafood).flow(
    spread(by="lake", dir="x"),
    stack(by="species", dir="x"),
).mark(rect(h="count", fill="species"))

Notes ​

  • dir is required — stack() raises a ValueError without it.
  • Combine with coord=clock() on chart to turn a stack into a pie chart.
  • Use spread when you want gaps between groups.