1

I have multiple datasets with different size and I want to plot a violin plot from them. My dataset looks like below:

Input.CSV:

         city_A city_B  city C  city_D
cluster1   2       5      4      4
cluster2   3       3      2      8
cluster3   2       4      5      5
cluster4   3       5      4
cluster5           3      3
cluster6           5

Note: Each city has a different size and number of clusters.

I looked into a few posts such as here and I could not understand how to plot this dataset in one plot like:

enter image description here

Some of the example from seaborn or matplotlib is with fake data and my data is in CSV format as I showed above. It would be great if you can provide your help with code that use data like mine.

1 Answer 1

2

If you have multiple lists you want to plot, you can put them as list of lists and plot them. You can read the documentation here https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.violinplot.html

from matplotlib import pyplot as plt

A = [2, 5, 6, 10, 12, 8, 5]
B = [2, 2, 6,  8, 14, 5, 5]
C = [5, 7, 5, 13, 17, 7, 5]
D = [1, 4, 7, 12, 12, 5, 5]
E = [4, 1, 2, 11, 13, 7, 5]

fig, ax = plt.subplots(figsize=(5,5))
ax.violinplot([A,B,C,D,E][::-1],positions =[5,4,3,2,1],vert=False,showmeans=True)

def set_axis_style(ax, labels):
    ax.get_yaxis().set_tick_params(direction='out')
    ax.xaxis.set_ticks_position('bottom')
    ax.set_yticks(np.arange(1, len(labels) + 1))
    ax.set_yticklabels(labels)
    ax.set_ylim(0.25, len(labels) + 0.75)
    ax.set_ylabel('Sample name')

set_axis_style(ax,['A','B','C','D','E'][::-1])

enter image description here

Seaborn looks like a better and more aesthetic solution for the dataframe.

from matplotlib import pyplot as plt
import seaborn as sns

fig, axes = plt.subplots(figsize=(5,5))
sns.set(style="whitegrid")
sns.violinplot(data=df, ax = axes, orient ='h')

enter image description here

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

9 Comments

thanks for your code. How I can add the group names A, B, C,D,E in Y axis and also arranges them in order from top to bottom? now only showing 1 to 5 in your code. thanks
Edited for the groupnames but what do you mean by ordering them from top to bottom. What is your order based on?
thanks a lot. I mean by order is, shows from top ``` A`` to bottom E
Oh so A should be on the top and E on the bottom?
Yes. Because i have other plots which they are in order of from top A to bottom E. Also is it possible that i load my data from dataframe likedf = pd.read_csv('test.csv'). I f i can, then how i should put data in CSV that gives me this plot. Thanks for your help
|

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.