Skip to content

GridY

plotynium.marks.GridY

GridY(data=None, x1=None, x2=None, y=None, stroke=None, stroke_opacity=0.1, stroke_width=1, stroke_dasharray=None)

Marker for adding horizontal lines from y ticks.

Parameters:

Name Type Description Default

data

list[T]

List of y positions where horizontal lines will be placed.

None

x1

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

X accessor function or key value for tail coordinates of the lines

None

x2

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

X accessor function or key value for head coordinates of the lines

None

y

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

Y accessor function or key value

None

stroke

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

Function which takes a data and returns a color applied for stroke attribute.

None

stroke_width

float

Stroke width value.

1

stroke_opacity

float

Stroke opacity value included in [0, 1].

0.1

stroke_dasharray

str | None

Stroke dasharray value.

None
Source code in plotynium/marks/grid.py
def __init__(
    self,
    data: list[T] | None = None,
    x1: Callable[[T], Data] | None = None,
    x2: Callable[[T], Data] | None = None,
    y: Callable[[T], Data] | str | None = None,
    stroke: str | None = None,
    stroke_opacity: float = 0.1,
    stroke_width: float = 1,
    stroke_dasharray: str | None = None,
):
    Mark.__init__(self)
    self._data = data or d3.ticks(0, 1, 10)
    self._x1 = x1
    self._x2 = x2
    self._y = y or Identity()
    self._stroke = stroke or "currentColor"
    self._stroke_opacity = stroke_opacity
    self._stroke_width = stroke_width
    self._stroke_dasharray = stroke_dasharray

    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_scaler_type instance-attribute

y_scaler_type = determine_scaler(_data, _y)

apply

apply(svg, ctx)

Add horizontal lines from y ticks.

Parameters:

Name Type Description Default

svg

Selection

SVG Content

required

ctx

Context

Context

required
Source code in plotynium/marks/grid.py
def apply(self, svg: Selection, ctx: Context):
    """
    Add horizontal lines from y ticks.

    Parameters
    ----------
    svg : Selection
        SVG Content
    ctx : Context
        Context
    """
    width = ctx.width
    margin_right = ctx.margin.right
    margin_left = ctx.margin.left
    x1 = self._x1 or Constant(margin_left)
    x2 = self._x2 or Constant(width - margin_right)
    g = svg.append("g").attr("aria-label", "y-grid").attr("stroke", self._stroke)
    if self._stroke_width:
        g.attr("stroke_width", self._stroke_width)
    if self._stroke_dasharray:
        g.attr("stroke-dasharray", self._stroke_dasharray)
    if self._stroke_opacity:
        g.attr("stroke-opacity", self._stroke_opacity)

    (
        g.select_all("line")
        .data(self._data)
        .join("line")
        .attr("x1", x1)
        .attr("x2", x2)
        .attr("y1", lambda d: ctx.y(d))
        .attr("y2", lambda d: ctx.y(d))
    )