Skip to content

[Doc]: Use tick_params() instead of iterating over tick labels for setting tick properties #23372

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

Open
timhoffm opened this issue Jun 30, 2022 · 3 comments

Comments

@timhoffm
Copy link
Member

Documentation Link

No response

Problem

At first sight, the effects of (1)

ax.tick_params(axis='x', labelrotation=90)

and (2)

for label in ax.get_xticklabels():
    label.set_rotation(90)

look similar.

However, the first solution is to be preferred:

  • It's shorter.
  • There's a fundamental difference: Tick label instances are not stable. They may be deleted, moved around or new ones created, e.g. when you pan or zoom the plot.(1) affects all current tick labels and sets the default for future ones. (2) only modifies the current labels. That means you can get labels with a different style when you manipulate the figure. This is almost never wanted. Therefore (2) is discouraged and we should remove it from our examples.

Suggested improvement

  1. Search through the examples and tutorials files. Suggest regular expression in.*ticklabels\(\)
  2. Replace the for loop with a call to tick_params(). (Note: Not every property of ticks is accessible through tick_params() so there may be some rare cases that cannot be replaced as of now.)
  3. Validate visually that the old and new code produce the same figure.
@timhoffm timhoffm added Documentation Good first issue Open a pull request against these issues if there are no active ones! labels Jun 30, 2022
@QuLogic
Copy link
Member

QuLogic commented Jun 30, 2022

Possibly also iteration over Axis.get_major_ticks and Axes.get_minor_ticks should be changed as well.

@QuLogic
Copy link
Member

QuLogic commented Jun 30, 2022

Also, many of these calls set rotation, and then also horizontalalignment, but I don't think we expose that via tick_params, so this is somewhat contingent on #20644

QuLogic added a commit to QuLogic/matplotlib that referenced this issue Jun 30, 2022
Related to, but does not completely fix, matplotlib#23372.
QuLogic added a commit to QuLogic/matplotlib that referenced this issue Jul 1, 2022
Related to, but does not completely fix, matplotlib#23372.
@timhoffm timhoffm removed the Good first issue Open a pull request against these issues if there are no active ones! label Jul 1, 2022
@timhoffm
Copy link
Member Author

timhoffm commented Jul 1, 2022

Simple cases fixed by #23377.

waiting for #20644

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants