Skip to content

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

Open
timtreis wants to merge 2 commits into
mainfrom
fix/issue-364
Open

fix(labels): separate per-render categorical legends with distinct palettes#723
timtreis wants to merge 2 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

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.31325% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.14%. Comparing base (b4ad6f7) to head (77299ce).

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/_color.py 53.33% 6 Missing and 1 partial ⚠️
src/spatialdata_plot/pl/utils.py 76.00% 2 Missing and 4 partials ⚠️
src/spatialdata_plot/pl/basic.py 88.37% 0 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #723      +/-   ##
==========================================
+ Coverage   79.09%   79.14%   +0.05%     
==========================================
  Files          17       17              
  Lines        4467     4532      +65     
  Branches      999     1015      +16     
==========================================
+ Hits         3533     3587      +54     
- Misses        593      595       +2     
- Partials      341      350       +9     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/render.py 89.31% <ø> (ø)
src/spatialdata_plot/pl/basic.py 82.96% <88.37%> (+0.27%) ⬆️
src/spatialdata_plot/pl/utils.py 78.99% <76.00%> (-0.25%) ⬇️
src/spatialdata_plot/pl/_color.py 68.50% <53.33%> (+0.37%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…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.
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