seaborn
seaborn copied to clipboard
BUG: `seaborn.axisgrid.FacetGrid.set_xticklabels(rotation=70)` makes x-ticks disappear
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
BUG: Result with Rotation
sns.__version__
Out[3]: '0.12.2'
matplotlib.__version__
Out[6]: '3.7.1'
Related: #1126, #2768, #2717, #3147, #3027, #2977, #3235
Maybe this PR is at fault: #1313
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()
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 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.