Skip to content

Commit c8f3e0e

Browse files
committed
Keep relative pad for colorbar sides without decorations (#687)
Use the absolute (clearance-aware) pad only where it's needed — sides with ticks/labels/title to clear (left/top/bottom) and stacked colorbars. Sides with negligible clearance (typically the default right) keep the relative pad, which matches the historical placement, so single right-side colorbars stay visually unchanged and baseline churn is limited to the genuinely-affected colorbars.
1 parent 5aa2988 commit c8f3e0e

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/spatialdata_plot/pl/basic.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
# once https://github.com/scverse/spatialdata/pull/689/ is in a release
8787
ColorLike = tuple[float, ...] | list[float] | str
8888

89+
# Below this clearance (inches), a colorbar side is treated as having no axis decorations to clear,
90+
# so the colorbar keeps its relative pad (matching the historical placement) instead of an absolute one.
91+
_CBAR_DECORATION_EPS_INCHES = 0.05
92+
8993

9094
@register_spatial_data_accessor("pl")
9195
class PlotAccessor:
@@ -1566,13 +1570,23 @@ def _draw_colorbar(
15661570
# side is padded past the panel's own decorations (ticks/labels/title) plus the requested
15671571
# pad; each subsequent one uses an absolute pad wide enough to clear the previous
15681572
# colorbar's tick labels instead of overlapping them.
1573+
# Sides with no decorations to clear (typically "right") keep the relative pad, which
1574+
# matches the historical placement; sides whose ticks/labels/title must be cleared
1575+
# (left/top/bottom), and stacked colorbars, use an absolute pad in inches.
15691576
n_on_side = side_counts.get(location, 0)
15701577
ref_in = axes_size_in[0] if location in {"left", "right"} else axes_size_in[1]
1571-
pad_in = CBAR_STACK_PAD_INCHES if n_on_side else clearance.get(location, 0.0) + max(pad, 0.0) * ref_in
1578+
side_clearance = clearance.get(location, 0.0)
1579+
pad_spec: str | Fixed
1580+
if n_on_side:
1581+
pad_spec = Fixed(CBAR_STACK_PAD_INCHES)
1582+
elif side_clearance > _CBAR_DECORATION_EPS_INCHES:
1583+
pad_spec = Fixed(side_clearance + max(pad, 0.0) * ref_in)
1584+
else:
1585+
pad_spec = f"{max(pad, 0.0) * 100}%"
15721586
cax = divider.append_axes(
15731587
location,
15741588
size=f"{max(fraction, 0.0) * 100}%",
1575-
pad=Fixed(pad_in),
1589+
pad=pad_spec,
15761590
axes_class=Axes,
15771591
)
15781592
side_counts[location] = n_on_side + 1

0 commit comments

Comments
 (0)