assign a color to a specific box in seaborn.boxplot

前端 未结 1 704
失恋的感觉
失恋的感觉 2020-12-14 23:38

I\'m calling seaborn.boxplot roughly as follows:

   seaborn.boxplot(ax=ax1,
                    x=\"centrality\", y=\"score\", hue=\"model\", data=data], 
           


        
相关标签:
1条回答
  • 2020-12-15 00:03

    The boxes made using sns.boxplot are really just matplotlib.patches.PathPatch objects. These are stored in ax.artists as a list.

    So, we can select one box in particular by indexing ax.artists. Then, you can set the facecolor, edgecolor and linewidth, among many other properties.

    For example (based on one of the examples 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()
    

    0 讨论(0)
提交回复
热议问题