Violin plot: one violin, two halves by Boolean value

Question:

I am toying around with seaborn violinplot, trying to make a single "violin" with each half being a different distribution, to be easily compared.

Modifying the simple example from here by changing the x axis to x=smoker I got to the following graph (linked below).

import seaborn as sns
sns.set(style="whitegrid", palette="pastel", color_codes=True)

# Load the example tips dataset
tips = sns.load_dataset("tips")

# Draw a nested violinplot and split the violins for easier comparison
sns.violinplot(x="smoker", y="total_bill", hue="smoker",
               split=True, inner="quart", data=tips)
sns.despine(left=True)

This is the resulting graph

I would like that the graph does not show two separated halves, just one single violin with two different distributions and colors.

Is it possible to do this with seaborn? Or maybe with other library?

Asked By: MonoColorado

||

Answers:

This is because you are specifying two things for the x axis with this line x="smoker". Namely, that it plot smoker yes and smoker no.

What you really want to do is plot all data. To do this you can just specify a single value for the x axis.

sns.set(style="whitegrid", palette="pastel", color_codes=True)

# Load the example tips dataset
tips = sns.load_dataset("tips")

# Draw a nested violinplot and split the violins for easier comparison
sns.violinplot(x=['Data']*len(tips),y="total_bill", hue="smoker",
               split=True, inner="quart",
               palette={"Yes": "y", "No": "b"},
               data=tips)
sns.despine(left=True)

This outputs the following:
output

Answered By: gnodab