fix: anchor sc.pl.scatter colorbar to user-supplied ax#4137
Open
gkneighb wants to merge 2 commits into
Open
Conversation
When sc.pl.scatter is called with a user-supplied ax, scatter_base created the colorbar with fig.add_axes(rectangle) using rectangle coordinates derived from panel_pos -- positions computed against scanpy's own panel layout, which has no relationship to the caller's figure. Result: the colorbar lands somewhere in the figure interior, typically overlapping a sibling user-axes. Detect the user-supplied-ax case at the top of scatter_base and route that branch through plt.colorbar(sct, ax=ax, ...), mirroring how sc.pl.embedding already handles its colorbar. The original rectangle-based path is preserved for the scanpy-managed-figure case so the visual layout of multi-panel scatter outputs stays unchanged. Adds tests/test_plotting.py::test_scatter_colorbar_uses_user_ax as a structural regression test that asserts the colorbar sits just to the right of the user ax, not floating across the figure. AI-assisted by Claude Code. Closes scverse#3963
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4137 +/- ##
=======================================
Coverage 79.61% 79.61%
=======================================
Files 120 120
Lines 12786 12789 +3
=======================================
+ Hits 10179 10182 +3
Misses 2607 2607
Flags with carried forward coverage won't be shown. Click here to find out more.
|
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.
Summary
Fixes #3963.
When
sc.pl.scatteris called with a user-suppliedax,scatter_baseplaced the colorbar byfig.add_axes(rectangle)with the rectangle coordinates computed frompanel_pos-- positions scanpy uses for its own internal panel layout. Those positions have no relationship to the caller's figure, so the colorbar lands somewhere in the figure interior, typically overlapping a sibling axes.sc.pl.embeddingalready handles this correctly by routing throughplt.colorbar(sct, ax=ax, ...). This PR detects the user-supplied-ax case inscatter_baseand uses the sameax=form. The original rectangle-based path is kept for the scanpy-managed-figure case, so multi-panel scatter outputs that scanpy lays out itself are visually unchanged.Before / after
The OP's repro pattern (left panel is
sc.pl.scatter, right panel is anything else):Before: colorbar floats between the two panels, partly over the right panel's y-axis labels.
After: colorbar sits directly to the right of the left ax, like every other matplotlib colorbar attached via
ax=.Locally on
pbmc3k_processedwithfig, axs = plt.subplots(1, 2, figsize=(10, 4)):x0=0.125 x1=0.446x0=0.449 x1=0.477(just past the user ax right edge)Test plan
Adds
tests/test_plotting.py::test_scatter_colorbar_uses_user_ax-- structural assertion that the colorbar's x position sits just past the user's ax right edge, rather than overlapping a sibling panel. Uses a tiny synthetic AnnData to keep the test self-contained.Unable to run the full
pytest tests/test_plotting.pylocally because every test in that file currently fails for me withCannot shard v2 format data. Please set anndata.settings.zarr_write_format to 3from theanndata_settingsautouse fixture (tests/conftest.py:162) interacting withanndata 0.12.10on Apple Silicon. The added test passes when run standalone with explicitad.settings.zarr_write_format = 3; ad.settings.auto_shard_zarr_v3 = False, and the underlying fix is verified against the OP's two-panel repro. CI on a clean runner should be the authoritative check.Notes
<PR>.fix.mdin a follow-up commit here).AI-assisted by Claude Code.