Seaborn: Rotating xtick labels in Seaborn FacetGrid plots

Created on 17 Feb 2016  路  7Comments  路  Source: mwaskom/seaborn

Is there a way to rotate tick labels for individual facets in a FacetGrid? I have a case where I need to use sharex = False switch in FacetGrid. My xticklabels are rather long and start to overlap. I would like to rotate all the xticklabels to 90 degree rotation. I'm currently using the g.set_xticklabels(rotation = 90) but this only rotates the tick labels for the lowest facets. Is there a way to apply to all facets?

The example below is for illustration purposes only- I realize that there is no need for rotating tick labels in the facets below. I'm only intending to highlight what I want to do.

image

Most helpful comment

For future visitors, the above code snippet in text form:

for ax in g.axes.flat:
    for label in ax.get_yticklabels():
        label.set_rotation(0)

All 7 comments

Probably a bug in g.set_xticklabels that doesn't account for unshared axes (doing so can be a little tricky).

If you want to do it in a one-liner, you can do

[plt.setp(ax.get_xticklabels(), rotation=90) for ax in g.axes.flat]

Worked with a small modification to your suggestion. Not sure if I mis-interpreted your suggestion or just couldn't make it work, Either way, I was able to get what I wanted. Thanks for the help. Closing the issue

image

I can honestly hug you guys! I've been trying to figure this out for a while!!!!!

For future visitors, the above code snippet in text form:

for ax in g.axes.flat:
    for label in ax.get_yticklabels():
        label.set_rotation(0)

corrected the pasted code (y for x) and erased flat. This is for python 3.

for ax in g.axes:
for label in ax.get_xticklabels():
label.set_rotation(0)

good answer!

I find the following to be easiest:

for ax in g.axes.ravel():
    ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments

Bercio picture Bercio  路  3Comments

tritemio picture tritemio  路  3Comments

alexpetralia picture alexpetralia  路  3Comments

JanHomann picture JanHomann  路  3Comments