15

I'm calling seaborn.boxplot roughly as follows:

   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)

Is it possible to make one box stand out by giving it a specific color, while coloring all others with the given color palette?

enter image description here

clstaudt
  • 21,436
  • 45
  • 156
  • 239
  • 1
    Can you provide a minimal, complete and verifiable example? – Chiel Mar 30 '16 at 10:14
  • @Chiel the seaborn.boxplot docs provide a minimal example for you to play with: http://web.stanford.edu/~mwaskom/software/seaborn/generated/seaborn.boxplot.html – clstaudt Mar 30 '16 at 17:19

2 Answers2

33
  • The correct method is now .patches instead of artists. However, there are different types of patches, so it's not as simple as selecting the 3rd patch.
  • In this example, list(ax.patches) returns the following list:
    • The boxplot patches are the PathPatch objects. As such the correct patch, for the 3rd boxplot, is ax.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


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()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • how would you change the whiskers and mean color? mybox.set_whiskercolor() does not work – Simon Chemnitz-Thomsen Oct 18 '21 at 17:55
  • 1
    The whiskers, the caps and the mean lines are all stored in `ax.lines`. With some trial and error you could find which line corresponds to the mean you want to change, and then do `ax.lines[X].set_color('blue')` or similar (where X is the index of the line you want to change) – tmdavison Oct 18 '21 at 18:45
  • A little more detail: each box has 5 lines associated with it. So `ax.lines[0:5]` corresponds to the first box, `ax.lines[5:11]` corresponds to the second box, etc. `ax.lines[0:2]` will change the whiskers, `ax.lines[2:4]` will change the caps, and `ax.lines[4]` will change the mean line for the first box – tmdavison Oct 18 '21 at 18:50
  • great this worked fantastically – Simon Chemnitz-Thomsen Oct 18 '21 at 19:05
5

Seaborn uses Matplotlib under the hood. In matplotlib 3.5 the boxes are stored in boxes instead of artists..

As such, you can set the color of boxes like so:

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)
Mitchell van Zuylen
  • 3,905
  • 4
  • 27
  • 64