Stacks

detroit.stack()

Builds a new stack generator with the default settings.

class detroit.shape.stack.Stack

Builds a new stack generator with the default settings.

__call__(data: list[T], *args: Any) list[Series]

Generates a stack for the given array of data and returns an array representing each series.

Parameters:
  • data (list[T]) – List of data

  • *args (Any) – Additional arguments passed to keys method.

Returns:

List of series

Return type:

list[Series]

set_keys(keys: Callable[[...], list[str]] | list[str]) Stack

Sets the keys method and returns itself.

Parameters:

keys (Callable[[...], str] | list[str]) – List of keys or function which returns a list of keys

Returns:

Itself

Return type:

Stack

Notes

The arguments of the function are the same as the additional arguments of Stack.__call__ method.

set_value(value: Callable[[T, str, int, list[T]], float] | float) Stack

Sets the value method and returns itself.

Parameters:

value (Callable[[T, str, int, list[T]], float] | float) – Value number or function which takes the optional argument: data, key, index, list of data. The function must returns a float value.

Returns:

Itself

Return type:

Stack

Notes

You can pass a function with only the data and key arguments for example.

set_order(order: Callable[[list[Series]], list[int]] | list[int] | None = None) Stack

Sets the order method and returns itself.

Parameters:

order (Callable[[list[Series]], list[int]] | list[int] | None) – If none value, resets the method. List of indices or function which takes in argument a list of series and returns the order list.

Returns:

Itself

Return type:

Stack

set_offset(offset: Callable[[list[Series], list[int]], None] | None = None) Stack

Sets the offset and returns itself.

Parameters:

offset (Callable[[list[Series], list[int]], None] | None) – If none value, resets the method. Function which takes in arguments a list of series and the order list and updates lower and upper values int the series list.

Returns:

Itself

Return type:

Stack

Stack orders

detroit.stack_order_appearance(series: list[Series[T]]) list[int]

Returns a series order such that the earliest series (according to the maximum value) is at the bottom.

Parameters:

series (list[Series[T]]) – List of series

Returns:

List of ordered indices

Return type:

list[int]

../../_images/light-stack-order-appearance.svg ../../_images/dark-stack-order-appearance.svg
detroit.stack_order_ascending(series: list[Series[T]]) list[int]

Returns a series order such that the smallest series (according to the sum of values) is at the bottom.

Parameters:

series (list[Series[T]]) – List of series

Returns:

List of ordered indices

Return type:

list[int]

../../_images/light-stack-order-ascending.svg ../../_images/dark-stack-order-ascending.svg
detroit.stack_order_descending(series: list[Series[T]]) list[int]

Returns a series order such that the largest series (according to the sum of values) is at the bottom.

Parameters:

series (list[Series[T]]) – List of series

Returns:

List of ordered indices

Return type:

list[int]

../../_images/light-stack-order-descending.svg ../../_images/dark-stack-order-descending.svg
detroit.stack_order_inside_out(series: list[Series[T]]) list[int]

Returns a series order such that the earliest series (according to the maximum value) are on the inside and the later series are on the outside. This order is recommended for streamgraphs in conjunction with d3.stack_offset_wiggle.

Parameters:

series (list[Series[T]]) – List of series

Returns:

List of ordered indices

Return type:

list[int]

../../_images/light-stack-order-inside-out.svg ../../_images/dark-stack-order-inside-out.svg
detroit.stack_order_none(series: list[Series[T]]) list[int]

Returns the given series order \([0, 1, ..., n - 1]\) where \(n\) is the number of elements in series. Thus the stack is given by the key accessor.

Parameters:

series (list[Series[T]]) – List of series

Returns:

List of ordered indices

Return type:

list[int]

../../_images/light-stack-order-none.svg ../../_images/dark-stack-order-none.svg
detroit.stack_order_reverse(series: list[Series[T]]) list[int]

Returns the reverse of the given series order \([n - 1, n - 2, ..., 0]\) where \(n\) is the number of elements in series. Thus, the stack order is given by the reverse of the key accessor.

Parameters:

series (list[Series[T]]) – List of series

Returns:

Order list

Return type:

list[int]

../../_images/light-stack-order-reverse.svg ../../_images/dark-stack-order-reverse.svg

Stack offsets

detroit.stack_offset_expand(series: list[Series[T]], order: list[int])

Applies a zero baseline and normalizes the values for each point such that the topline is always one.

Parameters:
  • series (list[Series[T]]) – List of series

  • order (list[int]) – List of ordered indices

../../_images/light-stack-offset-expand.svg ../../_images/dark-stack-offset-expand.svg
detroit.stack_offset_diverging(series: list[Series[T]], order: list[int])

Positive values are stacked above zero, negative values are stacked below zero, and zero values are stacked at zero.

Parameters:
  • series (list[Series[T]]) – List of series

  • order (list[int]) – List of ordered indices

../../_images/light-stack-offset-diverging.svg ../../_images/dark-stack-offset-diverging.svg
detroit.stack_offset_none(series: list[Series[T]], order: list[int])

Applies a zero baseline.

Parameters:
  • series (list[Series[T]]) – List of series

  • order (list[int]) – List of ordered indices

../../_images/light-stack-offset-none.svg ../../_images/dark-stack-offset-none.svg
detroit.stack_offset_silhouette(series: list[Series[T]], order: list[int])

Shifts the baseline down such that the center of the streamgraph is always at zero.

Parameters:
  • series (list[Series[T]]) – List of series

  • order (list[int]) – List of ordered indices

../../_images/light-stack-offset-silhouette.svg ../../_images/dark-stack-offset-silhouette.svg
detroit.stack_offset_wiggle(series: list[Series[T]], order: list[int])

Shifts the baseline so as to minimize the weighted wiggle of layers. This offset is recommended for streamgraphs in conjunction with d3.stack_order_inside_out.

Parameters:
  • series (list[Series[T]]) – List of series

  • order (list[int]) – List of ordered indices

../../_images/light-stack-offset-wiggle.svg ../../_images/dark-stack-offset-wiggle.svg

Series

class detroit.shape.series.Serie(values: list[float], data: T)

Point of a Series

Parameters:
  • values (list[float]) – Point coordinates

  • data (T) – Data value associated to this point

__getitem__(index: int) float

Returns the value of a specific index

Parameters:

index (int) – Index value

Returns:

Value

Return type:

float

__setitem__(index: int, value: float)

Sets the value at a specific index

Parameters:
  • index (int) – Index value

  • value (float) – Value

__len__() int

Returns the length of the serie

Returns:

Length of serie

Return type:

int

__eq__(serie: Serie | Any) bool

Checks if another series value is the same as the current values.

Parameters:

serie (Serie[T] | Any) – Other serie

Returns:

True if two series are the same

Return type:

bool

__str__() str

Formats the class to a string.

Returns:

String representing the current class

Return type:

str

__repr__()

Return repr(self).

class detroit.shape.series.Series(series: list[Serie[T]] | None = None)

List of Serie associated to a key and an index

Parameters:

series (list[Serie[T]] | None) – List of series

__getitem__(index: int) Serie[T]

Gets a serie given an index

Parameters:

index (int) – Index value

Returns:

Serie value

Return type:

Serie[T]

__setitem__(index: int, serie: Serie[T])

Sets a serie at a specific index

Parameters:
  • index (int) – Index value

  • serie (Serie[T]) – Serie value

__len__() int

Returns the length of the serie

Returns:

Length

Return type:

int

__eq__(series: Series | Any) bool

Checks if another series value is the same as the current values.

Parameters:

series (Series[T] | Any) – Other series

Returns:

True if two series are the same

Return type:

bool

__str__() str

Formats the class to a string.

Returns:

String representing the current class

Return type:

str

__repr__()

Return repr(self).