Skip to content

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

Open
timtreis wants to merge 1 commit into
mainfrom
fix/issue-364
Open

fix(labels): separate per-render categorical legends with distinct palettes#723
timtreis wants to merge 1 commit 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 2 times, most recently from 9b0fe30 to 132b23f Compare June 17, 2026 15:03
…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>
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

1 participant