Make Axis.get_tick_space account for tick label rotation#32056
Make Axis.get_tick_space account for tick label rotation#32056m4nn2609-dot wants to merge 4 commits into
Conversation
|
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. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
|
Please show a visual before/after demonstration of this change |


PR summary
closes #32005
Axis.get_tick_space()estimates the number of ticks that fit on an axisby 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 thelinked issue's reproduction.
This PR makes the estimate aware of label rotation:
Added
Axis._get_tick_label_rotation(), which reads the rotation anglestored in
_major_tick_kw['labelrotation'](set viatick_params).XAxis.get_tick_space()andYAxis.get_tick_space()now blend theaspect-ratio heuristic between the horizontal and vertical cases using
the rotation angle:
aspect = |horizontal_factor * cos(theta)| + |vertical_factor * sin(theta)|At
theta = 0this is identical to the current behavior, so unrotatedlabels 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 aTickobjectfor performance reasons.
AI Disclosure
I used an AI assistant (Claude) as a tutor while working on this: to help
me understand how
get_tick_spaceand the tick-rotation storage in_major_tick_kwwork, to talk through possible approaches (including asimpler 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