fix(labels): separate per-render categorical legends with distinct palettes#723
Merged
Conversation
132b23f to
01120e5
Compare
…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 Report❌ Patch coverage is 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
🚀 New features to boost your workflow:
|
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #364.
Stacking categorical
render_labelscalls gave one merged legend (scanpy's bareax.legend()collected every artist and dropped the prior legend) with reused colors._sdata_column, so fill/outline & channel legends are untouched).