Skip to content
Advertisement

How to *Rotate* labels in a Seaborn PairGrid?

The columns in my dataframe has long names, so when I make a pairplot, the labels overlaps one another. I would like to rotate my labels 90 degrees, so they don’t collide. I tried looking up online and documentation, but could not find a solution. Here is something I wrote & the error message:

plt.figure(figsize=(10,10))
g = sn.pairplot(df, kind="scatter")
g.set_xticklabels(g.get_xticklabels(), rotation=90)
g.set_yticklabels(g.get_yticklabels(), rotation=90)
AttributeError: 'PairGrid' object has no attribute 'set_xticklabels'

How can I rotate labels (both x and y) in a Seaborn PairGrid?

Note: Sorry, my wifi can’t upload the image for reference.

Advertisement

Answer

Thanks to William’s answer, I now know what to look for to solve my problem!

Below is how I did it.

g = sn.pairplot(dfsub.sample(50), kind="scatter", hue=target)
for ax in g.axes.flatten():
    # rotate x axis labels
    ax.set_xlabel(ax.get_xlabel(), rotation = 90)
    # rotate y axis labels
    ax.set_ylabel(ax.get_ylabel(), rotation = 0)
    # set y labels alignment
    ax.yaxis.get_label().set_horizontalalignment('right')
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement