Skip to content

fix(labels): separate per-render categorical legends with distinct palettes#723

Merged
timtreis merged 7 commits into
mainfrom
fix/issue-364
Jun 19, 2026
Merged

fix(labels): separate per-render categorical legends with distinct palettes#723
timtreis merged 7 commits into
mainfrom
fix/issue-364

Conversation

@timtreis

@timtreis timtreis commented Jun 17, 2026

Copy link
Copy Markdown
Member

Fixes #364.

Stacking categorical render_labels calls gave one merged legend (scanpy's bare ax.legend() collected every artist and dropped the prior legend) with reused colors.

  • Each categorical render gets its own legend, laid left-to-right along the top margin (post-render pass; only legends tagged _sdata_column, so fill/outline & channel legends are untouched).
  • Later renders offset into the palette to skip already-used colors.
  • Legends auto-titled by column, matching colorbars.

@timtreis timtreis changed the title fix(labels): separate per-render categorical legends with distinct palettes (#364) fix(labels): separate per-render categorical legends with distinct palettes Jun 17, 2026
@timtreis timtreis force-pushed the fix/issue-364 branch 3 times, most recently from 132b23f to 01120e5 Compare June 17, 2026 16:25
…lettes (#364)

Stacking multiple categorical `render_labels` calls produced a single merged
legend (scanpy's bare `ax.legend()` auto-collected every labeled artist and
dropped the previous legend) and reused the same default palette, so the
overlaid layers were indistinguishable.

- Each categorical render now gets its own legend; 2nd+ legends are built with
  explicit handles and laid out left-to-right along the top margin in a
  post-render pass (`_layout_panel_legends_rightward`), robust to tall legends
  and `constrained_layout` reflow. Only legends this code owns (tagged
  `_sdata_column`) are touched, so fill/outline and channel legends keep their
  placement.
- Later categorical label renders offset into the palette to skip colors
  already used on the panel (`_next_palette_colors`), keeping layers distinct.
- A legend is titled by its column only when 2+ legends share an axis (to tell
  them apart); a lone legend stays untitled.
- Shared helpers `_legend_ncol`, `_categorical_legend_handles`, and
  `_default_categorical_palette` remove duplicated logic.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.44444% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.07%. Comparing base (b4ad6f7) to head (e294093).

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/_color.py 44.44% 8 Missing and 2 partials ⚠️
src/spatialdata_plot/pl/basic.py 84.09% 1 Missing and 6 partials ⚠️
src/spatialdata_plot/pl/utils.py 77.77% 2 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #723      +/-   ##
==========================================
- Coverage   79.09%   79.07%   -0.03%     
==========================================
  Files          17       17              
  Lines        4467     4539      +72     
  Branches      999     1017      +18     
==========================================
+ Hits         3533     3589      +56     
- Misses        593      598       +5     
- Partials      341      352      +11     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/render.py 89.31% <100.00%> (ø)
src/spatialdata_plot/pl/utils.py 79.06% <77.77%> (-0.18%) ⬇️
src/spatialdata_plot/pl/basic.py 82.59% <84.09%> (-0.10%) ⬇️
src/spatialdata_plot/pl/_color.py 68.15% <44.44%> (+0.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

timtreis and others added 6 commits June 18, 2026 17:02
…tically

The per-render categorical legends (#364) were laid left-to-right in
figure fraction with no width budget, so a 2nd legend overflowed the
figure's right edge (clipped, or rescued by bbox_inches=tight into a wide
gap). Stack them top-to-bottom in the right margin instead — the standard
right-margin convention — so wide legends grow figure height on save
rather than running off the right.

Also title a lone categorical legend by its source column (it previously
went through scanpy's untitled right-margin path), matching the colorbar
auto-title and the 2+-legend behavior.
Anchor the stacked per-render legends in axes-fraction (not figure-fraction):
when constrained_layout reserves right-margin space and rescales the axes, the
legends move with it instead of leaving a gap that shrinks the plot to a
thumbnail. Title each legend by its color column; assert palette offset
accumulates across 3 renders.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
#364)

Every categorical legend is now titled by its color column (consistent with
colorbars), and 2+ legends stack in the right margin; regenerate the affected
image baselines from CI's renderer.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A legend is a fixed-pixel artist, so its axes-fraction footprint shifts whenever
the axes is rescaled (constrained_layout, or a figure resize) — placing it once
is unstable and overlaps/squashes. Mirror what colorbars get for free from their
self-rescaling insets: recompute the side-by-side offsets on every draw via a
draw_event callback, measuring each legend at the geometry actually rendered.

Lone legends stay untitled in scanpy's default spot; 2+ are titled by their color
column and laid left-to-right. Revert the single-legend baselines (unchanged now)
to base; only the two two-element shapes plots change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…de legends (#364)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…cleanup

- Use the draw event's renderer (valid on PDF/SVG/PGF) instead of the Agg-only
  fig.canvas.get_renderer(); fixes a crash when saving a 2-legend plot to a
  vector format.
- Guard leg.set_loc() for matplotlib < 3.8 (matching render.py).
- Replace per-panel draw_event callbacks with a single connect-once figure-level
  dispatcher that repositions all panels (no unbounded re-registration on reuse).
- Reuse _categorical_legend_handles in _add_outline_legend; trim redundant
  comments; warn when the palette can't keep stacked legends distinct.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@timtreis timtreis merged commit 31158d3 into main Jun 19, 2026
7 of 8 checks passed
@timtreis timtreis deleted the fix/issue-364 branch June 19, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When multiple categories are plotted, only one legend is shown, and colors do not match

2 participants