Skip to content

Make Axis.get_tick_space account for tick label rotation#32056

Open
m4nn2609-dot wants to merge 4 commits into
matplotlib:mainfrom
m4nn2609-dot:fix-tick-space-rotation
Open

Make Axis.get_tick_space account for tick label rotation#32056
m4nn2609-dot wants to merge 4 commits into
matplotlib:mainfrom
m4nn2609-dot:fix-tick-space-rotation

Conversation

@m4nn2609-dot

@m4nn2609-dot m4nn2609-dot commented Jul 17, 2026

Copy link
Copy Markdown

PR summary

closes #32005

Axis.get_tick_space() estimates the number of ticks that fit on an axis
by dividing the available length by an estimated tick label size. That
estimate assumes tick labels are drawn horizontally (a fixed aspect-ratio
heuristic: 3:1 for the x-axis, 2:1 for the y-axis), so rotating labels via
tick_params(rotation=...) produces poor tick spacing, as shown in the
linked issue's reproduction.

This PR makes the estimate aware of label rotation:

  • Added Axis._get_tick_label_rotation(), which reads the rotation angle
    stored in _major_tick_kw['labelrotation'] (set via tick_params).

  • XAxis.get_tick_space() and YAxis.get_tick_space() now blend the
    aspect-ratio heuristic between the horizontal and vertical cases using
    the rotation angle:

    aspect = |horizontal_factor * cos(theta)| + |vertical_factor * sin(theta)|

    At theta = 0 this is identical to the current behavior, so unrotated
    labels are unaffected. As rotation approaches 90 degrees, the x-axis
    estimate shrinks towards a single line-height, and the y-axis estimate
    grows towards the horizontal-text ratio.

This keeps the estimate cheap (no actual text measurement is performed),
consistent with the existing design intent noted in
_get_tick_label_size's docstring, which avoids creating a Tick object
for performance reasons.

AI Disclosure

I used an AI assistant (Claude) as a tutor while working on this: to help
me understand how get_tick_space and the tick-rotation storage in
_major_tick_kw work, to talk through possible approaches (including a
simpler threshold-based alternative I considered and decided against),
and to help troubleshoot my local dev environment setup. I wrote and
reviewed the final diff myself and understand the reasoning behind it.

PR checklist

@github-actions

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@scottshambaugh

Copy link
Copy Markdown
Contributor

Please show a visual before/after demonstration of this change

@m4nn2609-dot

Copy link
Copy Markdown
Author
before_fix BEFORE after_fix AFTER

Comment thread lib/matplotlib/tests/test_axes.py Outdated
Comment thread lib/matplotlib/axis.py Outdated
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.

[Bug]: Axis.get_tick_space could take tick label rotation into account

3 participants