Skip to content

AxisY

plotynium.marks.AxisY

AxisY(data=None, x=None, y=None, anchor='left', label=None, fill=None, tick_rotate=0.0, tick_size=6, tick_format=None, stroke=None, stroke_opacity=1.0, stroke_width=1)

Marker for making an Y axis.

Parameters:

Name Type Description Default

data

list[T] | None

List of data for making Y axis

None

x

Callable[[T], Data] | str | None

X accessor function or key value

None

y

Callable[..., float] | str | None

Y accessor function or key value

None

anchor

Literal['left', 'right']

Anchor location, where "left" orientates ticks on left side and sets the axis position on the left of the plot whereas "right" orientates ticks on right side and sets the axis position on the right.

'left'

label

str | None

Label of the x axis.

None

fill

str | None

Fill color value.

None

tick_rotate

float

Tick rotation value.

0.0

tick_size

int

Tick size value.

6

tick_format

Callable[[T], str] | None

Tick format function which takes a data and returns a string.

None

stroke

str | None

Stroke color value.

None

stroke_opacity

float

Stroke opacity value included in [0, 1].

1.0

stroke_width

float

Stroke width value.

1
Source code in plotynium/marks/axis.py
def __init__(
    self,
    data: list[T] | None = None,
    x: Callable[..., float] | str | None = None,
    y: Callable[[T], Data] | str | None = None,
    anchor: Literal["left", "right"] = "left",
    label: str | None = None,
    fill: str | None = None,
    tick_rotate: float = 0.0,
    tick_size: int = 6,
    tick_format: Callable[[T], str] | None = None,
    stroke: str | None = None,
    stroke_opacity: float = 1.0,
    stroke_width: float = 1,
):
    Mark.__init__(self)
    self._data = data or d3.ticks(0, 1, 10)
    self._x = None if x is None else getter(x)
    self._y = y or Identity()
    self._anchor = anchor
    self._label = label
    self._fill = fill or "inherit"
    self._tick_rotate = tick_rotate
    self._tick_size = tick_size
    self._tick_format = tick_format if callable(tick_format) else Identity()
    self._stroke = stroke or "currentColor"
    self._stroke_opacity = stroke_opacity
    self._stroke_width = stroke_width

    self.y_label = self._label
    self.y_domain = domain(self._data, self._y)
    self.y_scaler_type = determine_scaler(self._data, self._y)

y_domain instance-attribute

y_domain = domain(_data, _y)

y_label instance-attribute

y_label = _label

y_scaler_type instance-attribute

y_scaler_type = determine_scaler(_data, _y)

apply

apply(svg, ctx)

Add Y axis to SVG content

Parameters:

Name Type Description Default

svg

Selection

SVG Content

required

ctx

Context

Context

required
Source code in plotynium/marks/axis.py
def apply(self, svg: Selection, ctx: Context):
    """
    Add Y axis to SVG content

    Parameters
    ----------
    svg : Selection
        SVG Content
    ctx : Context
        Context
    """
    x = ctx.x
    y = ctx.y

    dx = ctx.margin.left if self._anchor == "left" else ctx.width - ctx.margin.right
    x = self._x or Constant(dx)
    dir = -1 if self._anchor == "left" else 1

    if hasattr(y, "get_bandwidth"):
        offset = y.get_bandwidth() / 2
    else:
        offset = 0

    ticks = (
        svg.append("g")
        .attr("aria-label", "y-axis tick")
        .attr("stroke", self._stroke)
        .attr("fill", self._fill)
        .select_all("path")
        .data(self._data)
        .join("path")
        .attr("transform", lambda d: f"translate({x(d)}, {y(d) + offset})")
        .attr("d", f"M0,0L{dir * self._tick_size},0")
    )

    if self._stroke_opacity != 1.0:
        ticks.attr("stroke-opacity", self._stroke_opacity)
    if self._stroke_width != 1.0:
        ticks.attr("stroke-width", self._stroke_width)

    (
        svg.append("g")
        .attr("aria-label", "y-axis tick label")
        .attr("transform", f"translate({dir * (self._tick_size + 2.5)}, 0)")
        .attr("text-anchor", "end" if self._anchor == "left" else "start")
        .attr("fill", self._fill)
        .select_all("text")
        .data(self._data)
        .join("text")
        .attr("y", "0.32em")
        .attr("transform", lambda d: f"translate({x(d)}, {y(d) + offset})")
        .text(lambda d: str(self._tick_format(d)))
    )

    if self._label is not None:
        tx = -(y.get_range()[0] + y.get_range()[1]) // 2
        ty = (
            ctx.margin.left // 4
            if self._anchor == "left"
            else ctx.width - ctx.margin.right // 4
        )
        (
            svg.append("g")
            .attr("aria-label", "y-axis label")
            .attr("text-anchor", "middle")
            .attr("fill", self._fill)
            .attr("transform", "matrix(0 -1 1 0 0.5 0)")
            .append("text")
            .attr("transform", f"translate({tx}, {ty})")
            .text(self._label)
        )