Emac
Emac

Reputation: 1167

Seaborn Python xtick labels won't rotate

I'm sorry for asking this again because I know this has been asked several times before, and I've researched all of the other solutions that I can find on every other thread about it, but none of them work for me. No matter what I try, these xlabels still always come out horizontal and jumbled together. I don't know why mine is not working while others worked, but I suspect it has to do with the fact I have three subplots on the figure.

I have tried the following, and many other things as well:

ax1.set_xticklabels(rotation=30) <-- Nothing happens

axs[0].set_xticklabels(rotation=30) <-- Error

plt.xticks(rotation=45) <-- Nothing happens

Here is the code and the plots.

    customer_rev_df = pd.DataFrame(customer_rev, columns='Week Revenue Pieces Stops'.split())
    print(customer_rev_df.set_index('Week'))
    sns.set_style(style='whitegrid')
    fig, axs = plt.subplots(ncols=3, figsize=(16, 6))
    ax1 = sns.factorplot(x='Week', y='Revenue', data=customer_rev_df, ax=axs[0])
    ax2 = sns.factorplot(x='Week', y='Stops', data=customer_rev_df, ax=axs[1])
    ax3 = sns.factorplot(x='Week', y='Pieces', data=customer_rev_df, ax=axs[2])
    axs[0].set_ylabel('Revenue')
    axs[1].set_ylabel('Stops')
    axs[2].set_ylabel('Pieces')
    axs[0].set_title('Weekly Revenue')
    axs[1].set_title('Weekly Stops')
    axs[2].set_title('Weekly Pieces')
    plt.tight_layout()
    fig.show()

Example of my figure

I admit I'm still relatively new so go easy on me! Any help would appreciated. Thanks!

Upvotes: 1

Views: 2896

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339220

Setting the xticklabels with .set_xticklabels() requires to actually specify the xticklabels, like ax.set_xticklabels([1,2,3,4], rotation=30).

If you already have a nice plot and don't want to change the ticklabels themselves, you can do the following:

plt.setp(ax.get_xticklabels(), rotation=30)

where ax is the axes for which you want to rotate the xticklabels.

Upvotes: 5

Related Questions