11

I would like to combine this violin plot http://seaborn.pydata.org/generated/seaborn.violinplot.html (fourth example with split=True) with this one http://seaborn.pydata.org/examples/elaborate_violinplot.html.

Actually, I have a dataFrame with a column Success (Yes or No) and several data column. For example :

df = pd.DataFrame(
    {"Success": 50 * ["Yes"] + 50 * ["No"], 
     "A": np.random.randint(1, 7, 100), 
     "B": np.random.randint(1, 7, 100)}
)

    A  B Success
0   6  4     Yes
1   6  2     Yes
2   1  1     Yes
3   1  2     Yes
.. .. ..     ...
95  4  4      No
96  2  1      No
97  2  6      No
98  2  3      No
99  2  1      No

I would like to plot a violin plot for each data column. It works with :

import seaborn as sns
sns.violinplot(data=df[["A", "B"]], inner="quartile", bw=.15)

But now, I would like to split the violin according to the Success column. But, using hue="Success" I got an error with Cannot use 'hue' without 'x' or 'y'. Thus how can I do to plot the violin plot by splitting according to "Success" column ?

2 Answers 2

33

If understand your question correctly, you need to reshape your dataframe to have it in long format:

df = pd.melt(df, value_vars=['A', 'B'], id_vars='Success')
sns.violinplot(x='variable', y='value', hue='Success', data=df)
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

That is what I was looking for. Thanks
It does not seem to work anymore -- I mean running this code does not produce this image, but 4 violin plots
@CharlyEmpereur-mot to make it look like on this page you need to set the parameter split to True. From the docs: "when using hue nesting with a variable that takes two levels, setting split to True will draw half of a violin for each level. This can make it easier to directly compare the distributions"
1

I was able to adapt an example of a violin plot over a DataFrame like so:

df = pd.DataFrame({"Success": 50 * ["Yes"] + 50 * ["No"], 
                   "A": np.random.randint(1, 7, 100), 
                   "B": np.random.randint(1, 7, 100)})
sns.violinplot(df.A, df.B, df.Success, inner="quartile", split=True)
sns.plt.show()

Seaborn violin graph over Pandas DataFrame

Clearly, it still needs some work: the A scale should be sized to fit a single half-violin, for example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.