seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

BUG: `seaborn.axisgrid.FacetGrid.set_xticklabels(rotation=70)` makes x-ticks disappear

Open Demetrio92 opened this issue 1 year ago • 5 comments

Overview

Setting rotation of tick labels on a grid coming from an sns.catplot makes them disappear. Must be a combination of factors. Reproducible example below.

Reproducible Example:

import seaborn as sns

df = sns.load_dataset("penguins")

grid = sns.catplot(
    data=df,
    x='species',
    y='body_mass_g',
    row='island',
    hue='species',
    capsize=0.2,
    kind='bar',
    errorbar='pi',
    dodge=False,
    sharey=True,
    sharex=True,
)
# grid.figure.tight_layout()  # does not help
grid.set_xticklabels(rotation=70)  # turn off to see labels appear again
# grid.figure.tight_layout()  # does not help
grid.add_legend(title='species')
grid.figure.show()

Result without Rotation

image

BUG: Result with Rotation

image

Demetrio92 avatar Apr 25 '23 14:04 Demetrio92

sns.__version__
Out[3]: '0.12.2'

matplotlib.__version__
Out[6]: '3.7.1'

Demetrio92 avatar Apr 25 '23 14:04 Demetrio92

Related: #1126, #2768, #2717, #3147, #3027, #2977, #3235

Maybe this PR is at fault: #1313

Demetrio92 avatar Apr 25 '23 14:04 Demetrio92

WORKAROUND:

for ax in grid.axes.flat:
    ax.tick_params(axis='x', which='both', rotation=70)
grid.figure.tight_layout()  # here it does help
grid.add_legend(title='species')
grid.figure.show()

image

Demetrio92 avatar Apr 25 '23 14:04 Demetrio92

Maybe this PR is at fault: https://github.com/mwaskom/seaborn/pull/1313

This PR was not merged so I think you have to hold it blameless...

WORKAROUND

You don't actually need the for loop, as there is a tick_params method on the FacetGrid. This is in fact my recommended way of changing the label orientation.

mwaskom avatar Apr 25 '23 21:04 mwaskom

@mwaskom thanks, that works too. I'll now be using

grid.tick_params(axis='x', which='both', rotation=70)

I am not sure if the API should have two different ways of seemingly doing the same thing with a different result.

Demetrio92 avatar Apr 27 '23 12:04 Demetrio92