Skip to content

stacked_area

Mojo function 🡭

stacked_area

fn def stacked_area(df: DataFrame, category: String, series: String, value: String, smoothing: Float64 = 0, step: StepStyle = StepStyle.NONE, theme: Theme = Theme(WHITE, Color(UInt8(30), UInt8(100), UInt8(180), UInt8(255)), Color(UInt8(80), UInt8(80), UInt8(80), UInt8(255)), Color(UInt8(225), UInt8(225), UInt8(225), UInt8(255)), Color(UInt8(40), UInt8(40), UInt8(40), UInt8(255)), SIMD(12), SIMD(3.5), SIMD(2), Int(60), Int(20), Int(20), Int(50), True, False, False, Color(UInt8(240), UInt8(240), UInt8(240), UInt8(255)), Int(3), Color(UInt8(60), UInt8(110), UInt8(200), UInt8(255)), Color(UInt8(235), UInt8(235), UInt8(235), UInt8(255)), Color(UInt8(220), UInt8(90), UInt8(40), UInt8(255)), ColorRamp(), ColorPalette(), SIMD(3), SIMD(15), True, SIMD(1), Int(0), False, Color(UInt8(200), UInt8(60), UInt8(60), UInt8(255)), Color(UInt8(224), UInt8(224), UInt8(224), UInt8(255)), Color(UInt8(120), UInt8(120), UInt8(120), UInt8(255)), Color(UInt8(100), UInt8(100), UInt8(100), UInt8(255)), Color(UInt8(230), UInt8(230), UInt8(230), UInt8(255)), Color(UInt8(255), UInt8(255), UInt8(255), UInt8(255)), UInt8(90), UInt8(160), False, False, SIMD(0), SIMD(18), SIMD(14), Color(UInt8(110), UInt8(110), UInt8(110), UInt8(255)), SIMD(14), Color(UInt8(150), UInt8(150), UInt8(150), UInt8(255)), Color(UInt8(224), UInt8(236), UInt8(246), UInt8(200)), Color(UInt8(255), UInt8(255), UInt8(255), UInt8(255)), True, False, False, True, AxisPosition.EDGE, AxisPosition.EDGE, String("sans-serif"), True, UInt8(90), Int(5), Int(4), Int(130), Int(14), Int(8), Int(14), Int(100), Int(8), SIMD(4), OutputFormat.SVG, Tooltips.AUTO, Missing.DRAW, String("(missing)"), Int(1000), False, LineStyle.SOLID, SIMD(1.5), LineStyle.SOLID, LegendPosition.RIGHT, XAxisLabelRotation.AUTO, TickFormat.AUTO, TickFormat.AUTO), width: Int = Int(640), height: Int = Int(420), title: String = "", subtitle: String = "", x_title: String = "", y_title: String = "") -> Chart[Streamgraph]

stacked_area() over a long-form dataframe_mojo DataFrame (#743): one row per (series, category) pair, with value holding the cell.

A frame stores these long while the mark wants one row of values per series, so the rows are pivoted. Both orders are first appearance, and every series needs a value in every category: a missing pair would have to be invented as a zero, and a repeated one is ambiguous, so either raises. The axis titles default to the category and value column names.

Args:

  • df (DataFrame): The frame to read.
  • category (String): The string column naming each category.
  • series (String): The string column naming each band.
  • value (String): The numeric column holding each cell.
  • smoothing (Float64): How much each band curves; see the list overload.
  • step (StepStyle): Step (stairs) interpolation; see the list overload.
  • theme (Theme): Full styling knobs beyond this function’s own parameters.
  • width (Int): Pixel width of the returned Plot (.size()).
  • height (Int): Pixel height of the returned Plot (.size()).
  • title (String): The chart’s title, shown above the plot.
  • subtitle (String): A line under the title.
  • x_title (String): The x-axis caption; defaults to category.
  • y_title (String): The y-axis caption; defaults to value.

Returns:

Chart[Streamgraph]: The finished chart – unrendered.

Raises:

Error: A named column is missing, has the wrong dtype for its channel, has missing values, or the (series, category) pairs are not exactly one per cell.

fn def stacked_area[dtype: DType](categories: List[String], series_names: List[String], values: List[List[Scalar[dtype]]], theme: Theme = Theme(WHITE, Color(UInt8(30), UInt8(100), UInt8(180), UInt8(255)), Color(UInt8(80), UInt8(80), UInt8(80), UInt8(255)), Color(UInt8(225), UInt8(225), UInt8(225), UInt8(255)), Color(UInt8(40), UInt8(40), UInt8(40), UInt8(255)), SIMD(12), SIMD(3.5), SIMD(2), Int(60), Int(20), Int(20), Int(50), True, False, False, Color(UInt8(240), UInt8(240), UInt8(240), UInt8(255)), Int(3), Color(UInt8(60), UInt8(110), UInt8(200), UInt8(255)), Color(UInt8(235), UInt8(235), UInt8(235), UInt8(255)), Color(UInt8(220), UInt8(90), UInt8(40), UInt8(255)), ColorRamp(), ColorPalette(), SIMD(3), SIMD(15), True, SIMD(1), Int(0), False, Color(UInt8(200), UInt8(60), UInt8(60), UInt8(255)), Color(UInt8(224), UInt8(224), UInt8(224), UInt8(255)), Color(UInt8(120), UInt8(120), UInt8(120), UInt8(255)), Color(UInt8(100), UInt8(100), UInt8(100), UInt8(255)), Color(UInt8(230), UInt8(230), UInt8(230), UInt8(255)), Color(UInt8(255), UInt8(255), UInt8(255), UInt8(255)), UInt8(90), UInt8(160), False, False, SIMD(0), SIMD(18), SIMD(14), Color(UInt8(110), UInt8(110), UInt8(110), UInt8(255)), SIMD(14), Color(UInt8(150), UInt8(150), UInt8(150), UInt8(255)), Color(UInt8(224), UInt8(236), UInt8(246), UInt8(200)), Color(UInt8(255), UInt8(255), UInt8(255), UInt8(255)), True, False, False, True, AxisPosition.EDGE, AxisPosition.EDGE, String("sans-serif"), True, UInt8(90), Int(5), Int(4), Int(130), Int(14), Int(8), Int(14), Int(100), Int(8), SIMD(4), OutputFormat.SVG, Tooltips.AUTO, Missing.DRAW, String("(missing)"), Int(1000), False, LineStyle.SOLID, SIMD(1.5), LineStyle.SOLID, LegendPosition.RIGHT, XAxisLabelRotation.AUTO, TickFormat.AUTO, TickFormat.AUTO), smoothing: Float64 = 0, step: StepStyle = StepStyle.NONE, width: Int = Int(640), height: Int = Int(420), title: String = "", subtitle: String = "", x_title: String = "", y_title: String = "") -> Chart[Streamgraph]

A stacked area chart: the same series streamgraph() stacks, laid on a flat zero baseline instead of a centered one.

Mark.STREAMGRAPH with StackBaseline.ZERO. The two charts share all their machinery and differ only in where each category’s stack starts, but they answer different questions, so they get different names: a streamgraph shows composition changing over time when the totals are not the point, and this shows the totals – the bottom series sits on the axis and can be read against it, and the top edge is the running total.

Note smoothing defaults to 0.0 here, not streamgraph()’s 0.6. A streamgraph is meant to look like flowing water; a stacked area chart is meant to be read, and curving between categories invents values that are not in the data.

That default is also why step lives here and not on streamgraph(). Curving and stepping are mutually exclusive (_check_step_smoothing), so streamgraph(step=...) would raise on its own smoothing=0.6 default, which is a bad thing to hand a caller. It reads better as a scope decision than as a workaround: a stepped stream is a contradiction – the flowing silhouette is the whole point – while a stacked area chart is meant to be read, and a stacked quantity that is constant between readings is exactly what a staircase says. Plot.mark_streamgraph(step=...) is still there for a caller who wants a stepped WIGGLE baseline and sets Theme.line_smoothing = 0.0 themselves.

Example:

from dataviz import stacked_area
from dataviz import save

def main() raises:
    var years: List[String] = ["2020", "2021", "2022", "2023", "2024"]
    var sources: List[String] = ["Wind", "Solar", "Hydro"]
    var output: List[List[Int]] = [
        [30, 40, 55, 60, 72],
        [10, 18, 30, 45, 66],
        [22, 23, 21, 24, 25],
    ]

    var c = stacked_area(
        years, sources, output, title="Renewable output (TWh)"
    )
    save(c, "docs/src/examples/out_stacked_area.svg")

Parameters:

  • dtype (DType)

Args:

  • categories (List[String]): One position along the x-axis per entry, in the given order.
  • series_names (List[String]): One band per name, bottom to top, used as the legend key.
  • values (List[List[Scalar[dtype]]]): values[j] is series_names[j]’s value per category. Every value must be non-negative.
  • theme (Theme): Full styling knobs beyond this function’s own parameters (colors, margins, fonts, gridlines, …) – see Theme’s docstring.
  • smoothing (Float64): Sets theme.line_smoothing – how much each band’s edges curve, [0.0, 1.0]. Defaults to 0.0, straight segments between categories.
  • step (StepStyle): Where the riser between two categories sits – NONE (the default: straight segments), PRE (at the earlier category), MID (halfway) or POST (at the later one). Every band’s top and bottom edge steps together, so the stack still tiles; see StepStyle. Mutually exclusive with a non-zero smoothing, which raises.
  • width (Int): Pixel width of the returned Plot (.size()).
  • height (Int): Pixel height of the returned Plot (.size()).
  • title (String): The chart’s title, shown above the plot.
  • subtitle (String): A secondary line shown under the title.
  • x_title (String): The x-axis caption.
  • y_title (String): The y-axis caption.

Returns:

Chart[Streamgraph]: The finished chart – unrendered. Call save(plot, path) to write it (any of .svg/.png/.bmp), or render(plot)/render_svg(plot) for the explicit two-step.

Raises: