在seaborn.boxplot中为特定框指定颜色

16

我大致按以下方式调用seaborn.boxplot:

   seaborn.boxplot(ax=ax1,
                    x="centrality", y="score", hue="model", data=data], 
                    palette=seaborn.color_palette("husl", len(models) +1),
                    showfliers=False, 
                    hue_order=order,
                    linewidth=1.5)

在使用给定的颜色调色板为所有其他盒子上色的情况下,是否可能通过给一个盒子赋予特定的颜色来使其突出?

输入图片描述


2
你能提供一个最小、完整且可验证的示例吗? - Chiel
@Chiel,seaborn.boxplot文档为你提供了一个最小的示例供你尝试:http://web.stanford.edu/~mwaskom/software/seaborn/generated/seaborn.boxplot.html - clstaudt
2个回答

34
  • 正确的方法现在是.patches而不是artists。然而,有不同类型的patches,所以选择第3个patch并不简单。
  • 在这个例子中,list(ax.patches)返回以下列表:
    • 盒图的patches是PathPatch对象。因此,对于第3个盒图,正确的patchax.patches[4]
  • [<matplotlib.patches.Rectangle at 0x1fe73317910>,
     <matplotlib.patches.PathPatch at 0x1fe72508050>,
     <matplotlib.patches.Rectangle at 0x1fe76b497d0>,
     <matplotlib.patches.PathPatch at 0x1fe6c35cc90>,
     <matplotlib.patches.PathPatch at 0x1fe76db67d0>,
     <matplotlib.patches.PathPatch at 0x1fe6c35c610>,
     <matplotlib.patches.PathPatch at 0x1fe715d8d90>,
     <matplotlib.patches.PathPatch at 0x1fe6c31b0d0>,
     <matplotlib.patches.PathPatch at 0x1fe6c2f5a90>,
     <matplotlib.patches.PathPatch at 0x1fe74df6450>]
    

    tips = sns.load_dataset("tips")
    ax = sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set3")
    
    # Select which box you want to change    
    mybox = ax.patches[4]
    
    # Change the appearance of that box
    mybox.set_facecolor('red')
    mybox.set_edgecolor('black')
    mybox.set_linewidth(3)
    

    enter image description here


    使用sns.boxplot创建的箱子实际上只是matplotlib.patches.PathPatch对象。这些对象以列表形式存储在ax.artists中。
    因此,我们可以通过对ax.artists进行索引来选择特定的箱子。然后,您可以设置facecoloredgecolorlinewidth等许多其他属性。
    例如(基于其中一个示例here):
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    sns.set_style("whitegrid")
    tips = sns.load_dataset("tips")
    ax = sns.boxplot(x="day", y="total_bill", hue="smoker",
                     data=tips, palette="Set3")
    
    # Select which box you want to change    
    mybox = ax.artists[2]
    
    # Change the appearance of that box
    mybox.set_facecolor('red')
    mybox.set_edgecolor('black')
    mybox.set_linewidth(3)
    
    plt.show()
    

    enter image description here


    你会如何改变whiskers和mean color的颜色?mybox.set_whiskercolor()无法工作。 - Simon Chemnitz-Thomsen
    1
    胡须、帽子和平均线都存储在 ax.lines 中。通过一些试错,您可以找到对应于您想要更改的平均值的线条,然后执行 ax.lines[X].set_color('blue') 或类似操作(其中 X 是要更改的线条的索引)。 - tmdavison
    稍微详细说明一下:每个箱子都有与之相关联的5条线。因此,ax.lines[0:5] 对应于第一个箱子,ax.lines[5:11] 对应于第二个箱子,以此类推。ax.lines[0:2] 将更改盒须,ax.lines[2:4] 将更改盖帽,而 ax.lines[4] 将更改第一个箱子的平均线。 - tmdavison
    太棒了,这个运行得非常好。 - Simon Chemnitz-Thomsen

    5

    Seaborn使用Matplotlib作为底层库。在Matplotlib 3.5中,盒子被存储在boxes而不是artists中。详见此处。

    因此,您可以像下面这样设置盒子的颜色:

    ax = sns.boxplot(x="day", y="total_bill", hue="smoker",
                     data=tips, palette="Set3")
    
    # Select which box you want to change    
    mybox = ax.patches[2] # `patches` instead of `artists`
    
    # Change the appearance of that box
    mybox.set_facecolor('red')
    mybox.set_edgecolor('black')
    mybox.set_linewidth(3)
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接