#53 Control color of seaborn violinplot

Color is probably the first feature you want to control on your seaborn violinplot. Here I give 4 tricks to control it:

1/ Use a color palette
# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

# Use a color palette
sns.violinplot( x=df["species"], y=df["sepal_length"], palette="Blues")

 

2/ Uniform color
#dataset and library
import seaborn as sns
df = sns.load_dataset('iris')

# plot
sns.violinplot( x=df["species"], y=df["sepal_length"], color="skyblue") 
3/ Specify color of each group
#dataset and library
import seaborn as sns
df = sns.load_dataset('iris')

# Make a dictionary with one specific color per group:
my_pal = {"versicolor": "g", "setosa": "b", "virginica":"m"}

#plot it
sns.violinplot( x=df["species"], y=df["sepal_length"], palette=my_pal) 
4/ Highlight a group
#dataset and library
import seaborn as sns
df = sns.load_dataset('iris')

# make a vector of color: red for the interesting group, blue for others:
my_pal = {species: "r" if species == "versicolor" else "b" for species in df.species.unique()}

# make the plot
sns.violinplot( x=df["species"], y=df["sepal_length"], palette=my_pal)

	

Leave a Reply

Your email address will not be published.