1

I have a problem with a violin plot. I'm trying to create 11 violin plots corresponding to 11 numpy matrices. I used this code and the result is shown in the figure:

for i in range(0,len(diff)):
    sns.violinplot(y=diff[i],orient='v')

enter image description here

I want to split all the violin plots and put a label below them.

Thanks for your help.

2 Answers 2

1

Use matplotlib directly, instead of seaborn:

diff = np.array([np.random.normal(loc=i, size=(100,)) for i in range(10)])

fig, ax = plt.subplots()
for i in range(0,len(diff)):
    ax.violinplot(dataset=diff[i],positions=[i])

enter image description here

Or, more compact:

fig, ax = plt.subplots()
ax.violinplot(dataset=diff.T,positions=range(10))

If your numpy arrays are separate:

array1 = np.random.normal(loc=0, size=(100,))
array2 = np.random.normal(loc=1, size=(100,))
array3 = np.random.normal(loc=2, size=(100,))
array4 = np.random.normal(loc=3, size=(100,))
array5 = np.random.normal(loc=4, size=(100,))


fig, ax = plt.subplots()
for i,arr in enumerate([array1, array2, array3, array4, array5]):
    ax.violinplot(dataset=arr,positions=[i])
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the answer but i have a different problem, I want to create 11 a violin plot, this 11 violin plot come from 11 different numpy matrix, not one with 11 rows
is diff a list of vectors? Your notation diff[i] in your code seem to imply that
I've added another snippet for how to deal with several separate arrays
diff[i] is a list of numpy matrix
Then the first or last solutions that I propose should get you where you want, don't they?
|
1

Or if you want to use seaborn:

diff = np.array([np.random.normal(loc=i, size=(100,)) for i in range(10)])
sns.violinplot(data=diff.T)

enter image description here

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.