From 8258b376b0a28c228015f3437808f7679797f47c Mon Sep 17 00:00:00 2001 From: vitusbenson Date: Mon, 23 Nov 2026 01:26:35 +0100 Subject: [PATCH] fix(scorecard): room for multi-line column units below the heading --- CHANGELOG.md | 5 +++++ src/nicefigs/_scorecard.py | 7 +++++-- src/nicefigs/tables.py | 5 +++-- tests/test_scorecard.py | 9 +++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e85282..59937fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,11 @@ to [Semantic Versioning](https://semver.org/). ticks, and the colourbar is half the grid width with ticks at the band edges and pointed ends. - `outline_boxes` draws unfilled rectangles (`fill=False`). +### Fixed + +- `scorecard` reserves room for every line of a column unit, so a unit with a second line (an + uncertainty, say) no longer overlaps the heading above it. + ### Notes - `nicefigs.cmap("RdBu")` returns `RdBu_r` (red high), so `scorecard(reference=..., cmap="RdBu")` diff --git a/src/nicefigs/_scorecard.py b/src/nicefigs/_scorecard.py index cc8700f..e59a3bb 100644 --- a/src/nicefigs/_scorecard.py +++ b/src/nicefigs/_scorecard.py @@ -60,17 +60,20 @@ def wrap_heading(label: str, size: float, width_pt: float) -> str: def header_heights( - headings: Sequence[str], has_units: bool, has_groups: bool, scale: float + headings: Sequence[str], unit_lines: int, has_groups: bool, scale: float ) -> dict[str, float]: """Vertical positions (pt above the top of the grid) of the stacked header elements. + ``unit_lines`` is the most lines any column's unit spans (0 without units), so a unit with a + second line, such as an uncertainty, keeps clear of the heading above it. + Returns: ``units`` and ``heading`` (bottoms of those texts), ``rule`` and ``group`` (the group rule and the bottom of the group titles) and ``top`` (upper edge of the whole header). """ n_lines = max((h.count("\n") + 1 for h in headings), default=1) units = 3.0 * scale - heading = units + (FS_UNIT * _LINE * scale + 1.0 * scale if has_units else 0.0) + heading = units + (unit_lines * FS_UNIT * _LINE * scale + 1.0 * scale if unit_lines else 0.0) top = heading + n_lines * FS_HEAD * _LINE * 0.95 * scale + 1.0 * scale rule = group = top if has_groups: diff --git a/src/nicefigs/tables.py b/src/nicefigs/tables.py index 3bb651f..6bcbb20 100644 --- a/src/nicefigs/tables.py +++ b/src/nicefigs/tables.py @@ -388,6 +388,7 @@ def scorecard( s = font_scale heads_raw = [str(c) for c in col_labels] units = [prettify_unit((col_units or {}).get(c)) for c in col_labels] + unit_lines = max((u.count("\n") + 1 for u in units if u), default=0) barlabel = colorbar_label if barlabel is None: barlabel = ( @@ -411,7 +412,7 @@ def scorecard( def _plan(cell: float): heads = [wrap_heading(h, FS_HEAD * s, cell * 72.0 * 1.04) for h in heads_raw] - hh = header_heights(heads, any(units), col_groups is not None, s) + hh = header_heights(heads, unit_lines, col_groups is not None, s) top_in = (hh["top"] + 4.0 * s) / 72.0 + (th.fs_title * 1.6 / 72.0 if title else 0.0) grid_w, grid_h = total_w * cell, total_h * cell extra = max(0.0, 0.5 * (cbar_w - grid_w)) + 0.06 @@ -437,7 +438,7 @@ def _plan(cell: float): box = ax.get_position() cell_pt = max(fig.get_figwidth() * box.width * 72.0 / total_w, 1.0) heads = [wrap_heading(h, FS_HEAD * s, cell_pt * 1.04) for h in heads_raw] - hh = header_heights(heads, any(units), col_groups is not None, s) + hh = header_heights(heads, unit_lines, col_groups is not None, s) ax.set_xlim(0, total_w) ax.set_ylim(total_h, 0) diff --git a/tests/test_scorecard.py b/tests/test_scorecard.py index 4dcfb79..44fc536 100644 --- a/tests/test_scorecard.py +++ b/tests/test_scorecard.py @@ -289,6 +289,15 @@ def test_text_overlaps_are_clean_for_a_grouped_card_with_colourbar(): assert nf.check_text_overlaps(ax.figure) == [] +def test_a_unit_on_two_lines_keeps_clear_of_its_heading(): + ax = _card( + [[1.0, 2.0], [0.9, 2.2]], + reference_row="ref", + col_units={"c0": "ppm\n±0.100", "c1": "ppm"}, + ) + assert nf.check_text_overlaps(ax.figure) == [] + + def test_the_baseline_example_renders_without_text_problems_and_has_the_deck_look(): path = Path(__file__).parent.parent / "examples" / "34_scorecard_baseline.py" spec = importlib.util.spec_from_file_location("ex34", path)