【问题标题】:How to rank plot in seaborn boxplot如何在seaborn boxplot中对情节进行排名?
【发布时间】:2017-01-05 23:17:56
【问题描述】:

以下面的 seaborn boxplot 为例,来自https://stanford.edu/~mwaskom/software/seaborn/examples/horizontal_boxplot.html

import numpy as np
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)

# Load the example planets dataset
planets = sns.load_dataset("planets")

# Plot the orbital period with horizontal boxes
ax = sns.boxplot(x="distance", y="method", data=planets,
                 whis=np.inf, color="c")

# Add in points to show each observation
sns.stripplot(x="distance", y="method", data=planets,
              jitter=True, size=3, color=".3", linewidth=0)


# Make the quantitative axis logarithmic
ax.set_xscale("log")
sns.despine(trim=True)

是否可以对条目进行“排名”,从最高到最低(反之亦然)?在这个情节中,“astrometry”应该是最后一个条目,如果从大到小排列的话。

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    试试这个:

    import numpy as np
    import seaborn as sns
    sns.set(style="ticks", palette="muted", color_codes=True)
    
    # Load the example planets dataset
    planets = sns.load_dataset("planets")
    
    ranks = planets.groupby("method")["distance"].mean().fillna(0).sort_values()[::-1].index
    
    # Plot the orbital period with horizontal boxes
    ax = sns.boxplot(x="distance", y="method", data=planets,
                     whis=np.inf, color="c",  order = ranks)
    
    # Add in points to show each observation
    sns.stripplot(x="distance", y="method", data=planets,
                  jitter=True, size=3, color=".3", linewidth=0, order = ranks)
    
    # Make the quantitative axis logarithmic
    ax.set_xscale("log")
    sns.despine(trim=True)
    

    【讨论】:

      【解决方案2】:

      您可以在sns.boxplotsns.stripplot 函数中使用order 参数来订购您的“盒子”。这是如何执行此操作的示例(因为尚不完全清楚您所说的“从最大到最低”是什么意思,即您要根据哪个变量对条目进行排序,我假设您想根据总和对它们进行排序它们的“距离”值,如果您想根据不同的值对它们进行排序,您应该能够编辑解决方案以满足您的需求):

      import numpy as np
      import seaborn as sns
      sns.set(style="ticks", palette="muted", color_codes=True)
      
      # Load the example planets dataset
      planets = sns.load_dataset("planets")
      
      # Determine the order of boxes
      order = planets.groupby(by=["method"])["distance"].sum().iloc[::-1].index
      
      # Plot the orbital period with horizontal boxes
      ax = sns.boxplot(x="distance", y="method", data=planets,
                       order=order, whis=np.inf, color="c")
      
      # Add in points to show each observation
      sns.stripplot(x="distance", y="method", data=planets, order=order,
                    jitter=True, size=3, color=".3", linewidth=0)
      
      
      # Make the quantitative axis logarithmic
      ax.set_xscale("log")
      sns.despine(trim=True)
      

      修改后的代码(注意sns.boxplotsns.stripplot 函数中order 参数的使用)将生成下图:

      【讨论】:

      • 谢谢,order 参数效果很好。我不确定其他评论去了哪里......
      猜你喜欢
      • 2021-10-29
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-02
      相关资源
      最近更新 更多