1

How can I change the rotation of the titles in each Facet (subplot) of a seaborn figure-level plot.

I would like to rotate label at the top by 90 degrees, and remove label at the bottom.

clik here to see picture

import seaborn as sns
sns.set_theme(style="whitegrid")
df = sns.load_dataset("exercise")

# Draw a pointplot to show pulse as a function of three categorical factors
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet",
                capsize=.2, palette="YlGnBu_d", height=6, aspect=.75,
                kind="point", data=df)
g.despine(left=True)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
DAMIEN
  • 17
  • 3

1 Answers1

2
import seaborn as sns

df = sns.load_dataset("exercise")

# Draw a pointplot to show pulse as a function of three categorical factors
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet",
                capsize=.2, palette="YlGnBu_d", height=6, aspect=.75,
                kind="point", data=df)
g.despine(left=True)

# rotate the xtick labels
g.set_xticklabels(rotation=30)

# flatten the array of axes for easy iteration and usage
axes = g.axes.flat

# rotate the titles by iterating through each axes
for ax in axes:
    # get the current title
    title = ax.get_title()
    # set the title and rotate the text
    ax.set_title(title, rotation=90)
    
# remove the yticks from the right Facet
axes[1].tick_params(left=False)

# remove all the xlabels
g.set(xlabel=None) 

enter image description here


  • For axes-level plots, there's no need to iterate through axes, since there's only one.
  • Use .get_xticklabels with .set_xticklabels to rotate xtick labels.
import seaborn as sns

df = sns.load_dataset("exercise")

# Draw a pointplot to show pulse as a function of three categorical factors
ax = sns.pointplot(x="time", y="pulse", hue="kind", col="diet", capsize=.2, palette="YlGnBu_d", data=df)

# remove spines
ax.spines[['left', 'right', 'top']].set_visible(False)

# rotate the xtick labels
ax.set_xticklabels(labels=ax.get_xticklabels(), rotation=30)

# set the title and rotate the text
ax.set_title('Diet: Low/No Fat', rotation=90)

# move the legend
sns.move_legend(ax, bbox_to_anchor=(1.25, 0.5), loc='center right', frameon=False)

# remove the xlabels
ax.set(xlabel=None)

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158