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

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