Search code examples
pythonpandasmatplotlibplotlyviolin-plot

Violin plot using sequential rows as the Y-axis


I am trying to build a violin plot of the following data(CSV):

,Exp No.,Sensory accuracy(mm),Volume of Data points,Exp 1,Exp 2,Exp 3,Exp 4,Exp 5,Exp 6,Exp 7,Exp 8,Exp 9,Exp 10,Coefficient ,PPIP
0,1,10,3,9948,9998,9961,10042,10049,10029,9975,10020,9986,10002,34.07508050043479,0.4231902824036948
1,2,10,5,10012,10021,9991,10013,9993,10010,9995,9992,10007,10008,10.591401124392268,0.7024168537963643
2,3,10,7,10015,10002,9999,10013,10013,9995,10005,10007,10003,10016,7.223418704310154,0.7758332605676019
3,4,10,10,10019,10006,10006,10013,9995,10007,9996,10014,10002,10004,7.627435858647242,0.766226316659029
4,5,10,15,10010,10012,10014,10010,10014,10018,10006,10006,10009,10010,3.7252889522529364,0.8703132644393902
5,6,15,3,9972,10041,9953,10049,10005,9968,9978,10016,10054,10011,36.0248371112055,0.40966926227828393

Can perhaps be seen easier in the image

Data set image

The X axis is represented 'sensor accuracy' and 'volume of data points' column.

I want to find a way for the Y axis to be represented by the data from Exp 1-10, for each of the X-parameters.

Is there an easy way to do this without reformatting the table into individual experiment lines?


Solution

  • The question is ambiguous. I will try my best;

    Data

    enter image description here

    Apply df.melt to unpivot the df from wide format to long format and acquire the columns needed to plot violin plot

    df2=pd.melt(df, id_vars=['Sensory','volume'], value_vars=['Exp1', 'Exp2', 'Exp3', 'Exp4', 'Exp5', 'Exp6', 'Exp7', 'Exp8', 'Exp9', 'Exp10'])
    

    Result

    enter image description here

    Plot Violin.

    Violin takes three variables x=variable 1, y=variable 2 and hue=variable 3 where by variable 3 can be used to classify'group by split=True if it has two distinct elements

    You were not too definite. I plotted it as below. We can improve if needed.

    Ungrouped:

    ax = sns.violinplot(x="variable", y="value", hue="Sensory", data=df2, palette="muted")
    

    If you need it grouped by Sensory then:

    ax = sns.violinplot(x="variable", y="value", hue="Sensory", split=True, data=df2, palette="muted")