Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn, violin plot with one data per column

Tags:

python

seaborn

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 ?

like image 955
Ger Avatar asked Jan 10 '17 16:01

Ger


Video Answer


2 Answers

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

like image 56
Phlya Avatar answered Oct 27 '22 08:10

Phlya


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.

like image 21
Dan Ross Avatar answered Oct 27 '22 10:10

Dan Ross