Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

aydengemz
Copy link

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 through tick_params, making it more consistent with other tick label properties and easier to use.

Changes Made

  1. Added new parameters to tick_params:

    • labelhorizontalalignment: Controls the horizontal alignment of tick labels
    • labelverticalalignment: Controls the vertical alignment of tick labels
  2. Added validation for alignment parameters:

    • For x-axis: labelhorizontalalignment accepts 'left', 'center', 'right'
    • For y-axis: labelverticalalignment accepts 'top', 'center', 'bottom', 'baseline', 'center_baseline'
  3. Added tests in test_axis.py to verify the functionality:

    • Testing horizontal and vertical alignment for both x and y axes
    • Testing parameter validation
  4. Updated documentation:

    • Added new parameters to tick_params docstring
    • Added documentation in axis_api.rst
    • Added a "What's New" entry in tick_label_alignment.rst

Example Usage

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
# Set horizontal alignment for x-axis labels
ax.tick_params(axis='x', labelhorizontalalignment='right')
# Set vertical alignment for y-axis labels
ax.tick_params(axis='y', labelverticalalignment='top')

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:

  1. Providing a more intuitive way to set tick label alignment
  2. Making the API more consistent with other tick label properties
  3. Reducing the need for manual iteration over tick labels
  4. Following the principle of least astonishment - if a user specifies ax.tick_params(axis="y", horizontalalignment="left"), they would expect it to apply to the y-axis labels

Implementation Details

The implementation:

  1. Adds the new parameters to the _translate_tick_params method in Axis class
  2. Adds parameter validation in set_tick_params method
  3. Implements the alignment setting in the _apply_params method of the Tick class
  4. Maintains consistency with existing tick parameter translation system

PR checklist

  • "closes No support for horizontal alignment in tick_params?  #13774" and "closes add label alignment to tick_params #20644" are in the body of the PR description
  • New and changed code is tested
    • Added test_tick_label_alignment in test_axis.py
  • Plotting related features are demonstrated in an example
    • Added example in PR summary
  • New Features and API Changes are noted with a directive and release note
    • Added new parameters to tick_params docstring
    • Added documentation for new parameters
  • Documentation complies with general and docstring guidelines
    • Updated docstrings in axes/_base.py and axis.py
    • Added clear descriptions of new parameters

Copy link

@github-actions github-actions bot left a 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.

Copy link

@evelyn-lo evelyn-lo left a 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?).

@rcomer
Copy link
Member

rcomer commented May 5, 2025

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.
image

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

@rcomer rcomer marked this pull request as draft May 5, 2025 11:00
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.

add label alignment to tick_params No support for horizontal alignment in tick_params?
3 participants