|
6 | 6 | from types import SimpleNamespace as NS |
7 | 7 | from typing import TYPE_CHECKING, cast |
8 | 8 |
|
| 9 | +import numpy as np |
| 10 | + |
9 | 11 | from .._utils import ensure_xy_location, get_opposite_side |
10 | 12 | from .._utils.registry import Register |
11 | 13 | from ..themes.theme import theme as Theme |
12 | 14 |
|
13 | 15 | if TYPE_CHECKING: |
14 | | - from typing import Literal, Optional, TypeAlias |
| 16 | + from typing import Literal, Optional, Sequence, TypeAlias |
15 | 17 |
|
16 | 18 | import pandas as pd |
17 | 19 | from matplotlib.offsetbox import PackerBase |
@@ -176,6 +178,10 @@ class GuideElements: |
176 | 178 | theme: Theme |
177 | 179 | guide: guide |
178 | 180 |
|
| 181 | + @cached_property |
| 182 | + def text(self): |
| 183 | + raise NotImplementedError |
| 184 | + |
179 | 185 | def __post_init__(self): |
180 | 186 | self.guide_kind = type(self.guide).__name__.split("_")[-1] |
181 | 187 | self._elements_cls = GuideElements |
@@ -275,3 +281,33 @@ def is_horizontal(self) -> bool: |
275 | 281 | Whether the guide is horizontal |
276 | 282 | """ |
277 | 283 | return self.direction == "horizontal" |
| 284 | + |
| 285 | + def has(self, n: int) -> Sequence[str]: |
| 286 | + """ |
| 287 | + Horizontal alignments per legend text |
| 288 | + """ |
| 289 | + ha = self.text.ha |
| 290 | + if isinstance(ha, (list, tuple)): |
| 291 | + if len(ha) != n: |
| 292 | + raise ValueError( |
| 293 | + "If `ha` is a sequence, its length should match the " |
| 294 | + f"number of texts. ({len(ha)} != {n})" |
| 295 | + ) |
| 296 | + else: |
| 297 | + ha = (ha,) * n |
| 298 | + return ha |
| 299 | + |
| 300 | + def vas(self, n: int) -> Sequence[str]: |
| 301 | + """ |
| 302 | + Vertical alignments per legend texts |
| 303 | + """ |
| 304 | + va = self.text.va |
| 305 | + if isinstance(va, (list, tuple)): |
| 306 | + if len(va) != n: |
| 307 | + raise ValueError( |
| 308 | + "If `va` is a sequence, its length should match the " |
| 309 | + f"number of texts. ({len(va)} != {n})" |
| 310 | + ) |
| 311 | + else: |
| 312 | + va = (va,) * n |
| 313 | + return va |
0 commit comments