-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: Add tick label alignment parameters to tick_params #30009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
You can also join us on gitter 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Summary:
The pull request adds two new keyword arguments to tick_params: labelhorizontalalignment and labelverticalalignment, allowing users to control the alignment of tick labels. These are especially useful when axis='x' or axis='y', offering better control over label placement for improved readability.
What’s good:
The new parameters are clearly named and consistent with existing style conventions.
Good documentation: The new parameters are described in both the docstring and usage examples.
The update is minimally invasive and leverages the existing tick_params logic.
Temporary storage of the axis parameter in self._tick_params_axis for use in Axis.set_tick_params is a practical way to enable context-aware behavior.
Suggested Improvements:
Validation logic – While _api.check_in_list validates axis, there's no explicit validation of labelhorizontalalignment and labelverticalalignment. Consider checking that these are valid strings ('center', 'left', 'right', etc.) supported by Text.set_horizontalalignment.
Docstring enhancement – You might clarify that these alignment settings only apply if the axis supports tick labels and that both parameters are passed down to Axis.set_tick_params.
Edge case clarification – For axis='both', it's not clear how or whether both alignments are respected (e.g., is labelhorizontalalignment passed to y-axis?).
Thank you for the potential contribution. Unfortunately many tests are failing, including the one you added. I also ran your example code with your branch and got this - the label alignment appears to be unaffected, and we see ticks on the top and right spines that should not be there. Before opening pull requests, you should test changes locally to verify these kind of issue do not come up. I'm going to mark this as "draft" for now. Please mark it as "ready for review" when you are ready. In the meantime, if you need help, feel free to ask questions here. Or you may prefer to ask them in our incubator gitter room. Finally, if you are using AI for any aspect of this, please see https://matplotlib.org/devdocs/devel/contribute.html#restrictions-on-generative-ai-usage |
PR summary
This PR adds support for horizontal and vertical alignment of tick labels through the
tick_params
method, addressing issues #13774 and #20644. The implementation allows users to control the alignment of tick labels directly throughtick_params
, making it more consistent with other tick label properties and easier to use.Changes Made
Added new parameters to
tick_params
:labelhorizontalalignment
: Controls the horizontal alignment of tick labelslabelverticalalignment
: Controls the vertical alignment of tick labelsAdded validation for alignment parameters:
labelhorizontalalignment
accepts 'left', 'center', 'right'labelverticalalignment
accepts 'top', 'center', 'bottom', 'baseline', 'center_baseline'Added tests in
test_axis.py
to verify the functionality:Updated documentation:
tick_params
docstringaxis_api.rst
tick_label_alignment.rst
Example Usage
Why is this change necessary?
Currently, users need to iterate over tick labels to set their alignment, which is less convenient than using
tick_params
. This change makes the API more consistent and user-friendly by:ax.tick_params(axis="y", horizontalalignment="left")
, they would expect it to apply to the y-axis labelsImplementation Details
The implementation:
_translate_tick_params
method inAxis
classset_tick_params
method_apply_params
method of theTick
classPR checklist
test_tick_label_alignment
intest_axis.py
tick_params
docstringaxes/_base.py
andaxis.py