代码之家  ›  专栏  ›  技术社区  ›  Cmagelssen

无法在seaborn/matplotlib中旋转xlabel

  •  0
  • Cmagelssen  · 技术社区  · 4 年前

    我无法在Seaborn/Matplotlib中旋转XLabel。我试过很多不同的解决办法,但都没能解决。我在stackoverflow上看到了很多相关的问题,但它们对我来说并不管用。

    enter image description here

        @staticmethod
        def plotPrestasjon(plot):
        sns.set(style="darkgrid")
        ax = sns.catplot(x="COURSE", y="FINISH", hue="COURSE",
                         col="BIB#", data=plot, s=9, palette="Set2")
        ax.set(xlabel='COURSES', ylabel='FINISH (sec)')
        plt.show()
    

    我试过:

    ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
    

    0 回复  |  直到 4 年前
        1
  •  2
  •   Trenton McKinney ivirshup    4 年前
    • 设置xticklabels的正确方法 sns.catplot 根据文件,是与 .set_xticklabels 方法(.例如。 g.set_xticklabels(rotation=30) ).
    • 使用循环迭代 Axes 如果需要逐点进行更改,则应在 FacetGrid .
    • Building structured multi-plot grids
    • seaborn.FacetGrid
    • g ax 是一个 seaborn.axisgrid.FacetGrid
      • 迭代时 ax.axes.flat , axes <class 'matplotlib.axes._subplots.AxesSubplot'> .get_xticklabels() .
    import seaborn as sns
    
    # load data
    exercise = sns.load_dataset("exercise")
    
    # plot catplot
    g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
    
    # set rotation
    g.set_xticklabels(rotation=30)
    

    enter image description here

    • 使用 g.set_xticklabels(g.get_xticklabels(), rotation=30) AttributeError .
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-442-d1d39d8cc4f0> in <module>
          1 g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
    ----> 2 g.set_xticklabels(g.get_xticklabels(), rotation=30)
    
    AttributeError: 'FacetGrid' object has no attribute 'get_xticklabels'
    
        2
  •  1
  •   Trenton McKinney ivirshup    4 年前

    尝试遍历属于 FacetGrid :

    for axes in ax.axes.flat:
        axes.set_xticklabels(axes.get_xticklabels(), 
                             rotation=90, 
                             horizontalalignment='right')
    
    • 面网格 由一系列 Axes ,而有些方法看起来对整个“情节”来说非常直观,实际上是要在每个人身上使用的 . 搜索 轴线 documentation . 您会看到,当他们需要更改与每个单独的子图相关的特性时(例如,与整个图的图例相反),它们经常迭代它们。