python - Seaborn Python xtick 标签不会旋转

标签 python matplotlib seaborn

我很抱歉再次问这个问题,因为我知道这个问题之前已经被问过好几次了,并且我已经研究了我可以在所有其他线程上找到的所有其他解决方案,但它们都不适合我。无论我如何尝试,这些 xlabel 仍然总是水平排列并且混杂在一起。我不知道为什么我的不工作而其他人工作,但我怀疑这与我在图中有三个子图有关。

我尝试了以下方法以及许多其他方法:

ax1.set_xticklabels(rotation=30) <-- 什么也没发生

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

plt.xticks(rotation=45) <-- 什么也没发生

这是代码和绘图。

    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

我承认我还比较新,所以对我宽容一些!任何帮助将不胜感激。谢谢!

最佳答案

使用 .set_xticklabels() 设置 xticklabels需要实际指定 xticklabels,例如 ax.set_xticklabels([1,2,3,4], rotation=30)

如果您已经有了一个不错的绘图并且不想更改刻度标签本身,您可以执行以下操作:

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

哪里ax是您要旋转 xticklabel 的轴。

关于python - Seaborn Python xtick 标签不会旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943034/

相关文章:

Python Argparse : Issue with optional arguments which are negative numbers

numpy - 解释散点图的 np.polyfit 和 np.polyval

python - Matplotlib 文本对齐

python - 不规则间隔的热图

python - 来自枢轴的 seaborn 热图中的数据顺序

python - 如何在Python中编写设置路径的setup.py和其他安装文件?

python - 将文本文件中的单词列表转换为单词向量

python - Seaborn 因子绘制自定义错误栏而不是自举

python - 需要正则表达式才能在 python 中仅获取 Tably 名称和主键

python - 如何在matplotlib条形图后面绘制网格线