kino
kino

Reputation: 41

Plot numpy matrix as violinplot in python

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.

Upvotes: 1

Views: 2165

Answers (2)

My Work
My Work

Reputation: 2518

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

Upvotes: 1

Diziet Asahi
Diziet Asahi

Reputation: 40747

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])

Upvotes: 1

Related Questions