Skip to content

Fix y-label overlap with tick labels for non-default rotations#31463

Open
FazeelUsmani wants to merge 2 commits intomatplotlib:mainfrom
FazeelUsmani:19029-colorbar-label-overlap
Open

Fix y-label overlap with tick labels for non-default rotations#31463
FazeelUsmani wants to merge 2 commits intomatplotlib:mainfrom
FazeelUsmani:19029-colorbar-label-overlap

Conversation

@FazeelUsmani
Copy link
Copy Markdown
Contributor

@FazeelUsmani FazeelUsmani commented Apr 7, 2026

Fixes #19029.

YAxis._update_label_position was setting the label position assuming a 90° rotation, so a y-label with rotation=270 (most often hit via cbar.set_label(..., rotation=270)) ended up overlapping the tick labels. @timhoffm and @jklymak diagnosed this in the issue thread; this PR follows their suggested approach: in _update_label_position, after computing the tentative position, check the rendered label's actual extent and shift it outward by however much it overhangs into the tick label area. Rotation-agnostic.

The fix is a no-op for rotation=90, so existing image baselines are unaffected — @jklymak's "this will change many image tests" worry didn't pan out.

Tests

  • test_ylabel_no_overlap_with_ticklabels in test_axes.py — generic axis-layer regression, parametrized over 7 rotations × 2 label positions
  • test_align_ylabels_mixed_rotation in test_axes.py — guards against the per-axis overhang correction breaking fig.align_ylabels() when sibling axes use different rotations (the visible bboxes still match, even though the internal anchor positions differ)
  • test_colorbar_label_rotation_no_overlap in test_colorbar.py — smoke test for the literal repro from the issue, via cbar.set_label('...', rotation=270)

Local result: 1217 passed, 86 skipped across test_axes, test_axis, test_colorbar, test_subplots, test_constrainedlayout, test_tightlayout, test_figure. No image baseline regressions.

…otlib#19029)

`YAxis._update_label_position` previously assumed a 90° label rotation
when computing the label's position, so y-labels with other rotations
(e.g. ``rotation=270`` for right-side colorbar labels) could overlap
the tick labels. The label position is now computed from the rendered
label's actual extent, so it sits clear of the tick labels for any
rotation. This implements @jklymak's suggestion in the issue thread of
checking the rendered label extent against the tick labels in
`_update_label_position`.

The fix is a no-op for the default ``rotation=90`` case, so existing
image baselines are unaffected.

Tests added in test_axes.py cover the generic axis-layer fix
(parametrized over 7 rotations × 2 label_positions) plus an
``align_ylabels`` regression that guards against the per-axis overhang
correction breaking sibling alignment when rotations differ. A
colorbar smoke test in test_colorbar.py exercises the original
user-facing symptom from the issue via ``cbar.set_label(...,
rotation=270)``.
@FazeelUsmani FazeelUsmani marked this pull request as draft April 7, 2026 10:26
@FazeelUsmani FazeelUsmani marked this pull request as ready for review April 7, 2026 14:21
@FazeelUsmani FazeelUsmani marked this pull request as draft April 7, 2026 14:49
@FazeelUsmani FazeelUsmani marked this pull request as ready for review April 9, 2026 08:02
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.

Default position of colorbar labels overlays its values

1 participant