Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")`
Expand Down
7 changes: 5 additions & 2 deletions src/nicefigs/_scorecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/nicefigs/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_scorecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading