Skip to content

[MNT]: Consider deprecating kwargs for set_xticklabels() #23162

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 May 29, 2022 · 5 comments
Open

[MNT]: Consider deprecating kwargs for set_xticklabels() #23162

timhoffm opened this issue May 29, 2022 · 5 comments

Comments

@timhoffm
Copy link
Member

timhoffm commented May 29, 2022

Summary

In #23158 and similar cases before people use

ticks = ax.get_xticks()
labels = ax.get_xticklabels()
ax.set_xticks(ticks)
ax.set_xticklabels(labels, rotation=90)

to change only the appearance of the ticks. Note that likely the get/set on ticks is inserted here because we have a warning that set_xticklabels chages to a fixed formatter and should only be used with after set_xticks.

The fundamental problem is that this changes to a fixed locator and formatter, which is an unintended side effect if I only want to rotate labels. Additionally, one can get caught up in the lazy determination of the ticks.

It's hard to distinguish between this sort of code and legitimate set_xticklabels calls. I don't think we can be more helpful with runtime warnings.

Proposed fix

A radical suggestion is to take away styling capability from set_xticklabels and instead direct people to tick_params(). The need for set_xticklabels (with it's major intent to set a fixed format - mainly for categorical values) is reduced since you can pass them as x values (plt.bar(['apples', 'peaches'], [1, 3])). I assume there are very few cases that people want a fixed formatter and adapt the style - which would be the only legitimate use for set_xticklabels(labels, rotation=90). They would need to go the extra mile and style separately (set_xticklabels(labels); tick_params('x', rotation=90)). But that might be worth it for removing quite a big foot canon.

@Tian-Jionglu
Copy link

@timhoffm Thanks for explanation.

@mwaskom
Copy link

mwaskom commented May 30, 2022

tick_params is not currently a replacement for set_{axis}ticklabels, right? It doesn't accept the arbitrary text kwargs. It's a little hard to follow all of the redirection, but one common task that I can't solve through tick_params is changing the label alignment after rotating.

@timhoffm
Copy link
Member Author

Thanks for the hint. It's indeed true that tick_params is not a full replacement. So the first step would be to add API (possibly in tick_params) to allow full customization of the (default, i.e. applying to all tick instances of an axis) label style parameters.

@mwaskom
Copy link

mwaskom commented May 30, 2022

IMO that would be useful whether or not this footgun gets deprecated!

@story645
Copy link
Member

So the first step would be to add API (possibly in tick_params) to allow full customization of the (default, i.e. applying to all tick instances of an axis) label style parameters.

xref #20644 since I opened that for label alignment & text properties in tick_params

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

5 participants