Cranjis
Cranjis

Reputation: 1960

seaborn plot violin of the arrays

I have two arrays:

l1 = [2,1,5,6,1,2,4,5,6,1]

l2 = [7,8,4,1,3,4,8]

I want to plot a seaborn violinplot with different color per list (a separate violin for l1 and l2). Is there a way to do so without creating a dataframe and pd.melt from them?

Upvotes: 1

Views: 482

Answers (1)

JohanC
JohanC

Reputation: 80329

You can give the parameters to sns.violinplot() without provding a dataframe, as follows:

import seaborn as sns

l1 = [2, 1, 5, 6, 1, 2, 4]
l2 = [7, 8, 4, 1, 3, 4, 8]
flag = [0, 1, 1, 1, 0, 0, 1]
sns.violinplot(y=l1 + l2, x=["l1"]*len(l1) + ["l2"]*len(l2),
               hue=flag + flag, palette=['crimson', 'cornflowerblue'])

violinplot for two arrays

To only use the l1 vs l2 information:

import seaborn as sns

l1 = [2, 1, 5, 6, 1, 2, 4]
l2 = [7, 8, 4, 1, 3, 4, 8]
sns.violinplot(y=l1 + l2, x=["l1"] * len(l1) + ["l2"] * len(l2), palette=['tomato', 'cornflowerblue'])

sns.violinplot from two lists

Upvotes: 1

Related Questions