Functional Weave
Code in Rust

charts.axis@1.0.1

impl/python/number_axis.py

1,122 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import Sequence

from .charts_axis_axis_from_ticks import axis_from_ticks  ← axisFromTicks, another function of this group · built into the same file, even by a slim install
from .charts_axis_types import Axis, AxisOrient
from .charts_format_format_tick import format_tick
from .charts_scale_linear_scale import linear_scale
from .charts_ticks_nice_ticks import nice_ticks
from .charts_ticks_tick_step import tick_step


def number_axis(
    domain: Sequence[float],
    range: Sequence[float],  # noqa: A002 - the manifest names it range
    count: int,
    orient: AxisOrient,
    position: float,
    tick_size: float,
) -> Axis:
    """A linear number axis: nice ticks, placed by linear_scale, labelled for the step."""
    if len(domain) != 2:
        raise ValueError("domain must have exactly 2 values, [from, to]; got %d" % len(domain))
    lo = min(domain[0], domain[1])
    hi = max(domain[0], domain[1])
    values = nice_ticks(domain[0], domain[1], count)
    step = tick_step(lo, hi, count)
    positions = [linear_scale(domain, range, v, False) for v in values]
    labels = [format_tick(v, step) for v in values]
    return axis_from_ticks(positions, labels, range, orient, position, tick_size)