Skip to content

Add channels_as_legend to render_images#576

Merged
timtreis merged 8 commits intomainfrom
feature/issue-459-channels-as-categories
Apr 8, 2026
Merged

Add channels_as_legend to render_images#576
timtreis merged 8 commits intomainfrom
feature/issue-459-channels-as-categories

Conversation

@timtreis
Copy link
Copy Markdown
Member

@timtreis timtreis commented Apr 3, 2026

Summary

  • Adds channels_as_categories: bool = False to render_images() — when True, shows a categorical legend mapping each channel name to its compositing color
  • Legend entries accumulate across chained render_images() calls and merge with labels/shapes legends on the same axes
  • Handles all multi-channel rendering paths: default RGB, palette, single cmap (sampled), and multi-cmap

Closes #459

Details

The feature follows the same deferred-draw pattern as colorbars: _render_images collects ChannelLegendEntry objects into a per-axis list, and _draw_channel_legend draws a single combined legend after the render loop completes. This avoids the matplotlib single-legend-per-axes limitation when chaining multiple render_images calls.

Edge cases handled:

  • Single channel / RGB(A): flag silently ignored (colorbar or native colors are more appropriate)
  • Single shared cmap across channels: legend skipped with a warning (channels aren't visually distinguishable)
  • Duplicate channel names: legend skipped with a warning
  • legend_loc="on data": falls back to "right margin" with a warning (no scatter coordinates for placement)
  • Multi-panel (multi_panel): avoids double-shrinking axes when a labels/shapes legend already applied the shrink

timtreis and others added 3 commits April 3, 2026 21:45
Add a `channels_as_categories: bool = False` parameter to
`render_images()` that shows a categorical legend mapping each
channel name to its compositing color for multi-channel images.

Legend entries are accumulated across chained `render_images()`
calls and drawn as a single combined legend after the render loop,
following the same deferred pattern as colorbars. When labels or
shapes also contribute legends on the same axes, the channel entries
merge automatically via matplotlib's artist-based legend collection.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace local macOS baselines with CI-generated (Linux) images for
the 5 existing channel legend tests.  Add 2 new visual tests for
legend positioning (upper left, lower right) to verify legend_loc
is respected by the channel legend.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace all 7 channel legend baselines with images generated on the
GitHub Actions runner (Linux) to match the CI rendering environment.

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

codecov-commenter commented Apr 3, 2026

Codecov Report

❌ Patch coverage is 82.22222% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.30%. Comparing base (f0f9e96) to head (deba903).

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/render.py 77.77% 5 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #576      +/-   ##
==========================================
+ Coverage   76.19%   76.30%   +0.11%     
==========================================
  Files          11       11              
  Lines        3188     3229      +41     
  Branches      750      757       +7     
==========================================
+ Hits         2429     2464      +35     
- Misses        461      465       +4     
- Partials      298      300       +2     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/basic.py 85.48% <100.00%> (+0.13%) ⬆️
src/spatialdata_plot/pl/render_params.py 86.63% <100.00%> (+0.33%) ⬆️
src/spatialdata_plot/pl/render.py 86.19% <77.77%> (-0.24%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@timtreis timtreis marked this pull request as ready for review April 8, 2026 08:22
@timtreis timtreis changed the title Add channels_as_categories to render_images Add channels_as_legend to render_images Apr 8, 2026
@timtreis timtreis merged commit f7a0f14 into main Apr 8, 2026
8 checks passed
@timtreis timtreis deleted the feature/issue-459-channels-as-categories branch April 8, 2026 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Showing legend by defaults and allowing richer customizability

2 participants