Chris
Chris

Reputation: 13660

Seaborn Boxplot with Same Color for All Boxes

I am using seaborn and want to generate a box plot where all boxes have the same color. For some reason seaborn uses different colors for each box and doesn't have an option to stop this behavior and set the same color for all boxes.

How can I force seaborn to use the same color for all boxes?

fig, ax = plt.subplots(figsize=(10, 20))
sns.boxplot(y='categorical_var', x='numeric_var', ax=ax)

Upvotes: 7

Views: 10034

Answers (2)

mwaskom
mwaskom

Reputation: 48992

Use the color parameter:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="tip", data=tips, color="seagreen")

enter image description here

Upvotes: 13

Serenity
Serenity

Reputation: 36635

Make your own palette and set the color of boxes like:

import seaborn as sns
import matplotlib.pylab as plt

sns.set_color_codes()
tips = sns.load_dataset("tips")
pal = {day: "b" for day in tips.day.unique()}
sns.boxplot(x="day", y="total_bill", data=tips, palette=pal)

plt.show()

enter image description here

Another way is to iterate over artists of boxplot and set the color with set_facecolor for every artist of axis istance:

ax = sns.boxplot(x="day", y="total_bill", data=tips)
for box in ax.artists:
    box.set_facecolor("green")

enter image description here

Upvotes: 2

Related Questions