unixsnob
unixsnob

Reputation: 1705

How to plot multiple groups of columns of a pandas dataframe side by side using Seaborn

The data repeats for multiple participants (p), each with several trials of which half fall under two bin_trials (1 and 2). This is just a small sample. I know that last column is missing, it was just too much data for the question.

       p   tr  bin_trial  cor_gaze_perc  cor_gaze_key_perc  
0      2    1          1           1.00               1.00   
1      2    2          1           1.00               1.00   
2      2    3          1           0.00               0.00   
3      2    4          1           0.50               0.50   
4      2    5          1           0.75               0.75   
5      2    6          1           0.50               0.50   
6      2    7          1           0.50               0.50   
7      2    8          1           0.25               0.25   
8      2    9          1           1.00               1.00   
9      2   10          1           0.50               0.50   
10     2   11          1           1.00               1.00   
11     2   12          1           1.00               1.00   
12     2   13          1           1.00               1.00   
13     2   14          1           1.00               1.00   
14     2   15          1           0.50               0.50   
15     2   16          2           1.00               1.00   
16     2   17          2           1.00               1.00   
17     2   18          2           1.00               1.00   
18     2   19          2           0.75               0.75   
19     2   20          2           1.00               1.00   
20     2   21          2           1.00               1.00   
21     2   22          2           1.00               1.00   
22     2   23          2           0.60               0.40   
23     2   24          2           1.00               1.00   
24     2   25          2           0.00               0.00   
25     2   26          2           0.00               0.00   
26     2   27          2           1.00               1.00   
27     2   28          2           0.00               0.00   
28     2   29          2           1.00               1.00   
29     2   30          2           0.50               0.50   

I want to achieve a plot that looks similar to this:

Plot I want done with Excel

I saw this previous answer Seaborn multiple barplots suggesting to use melt to then use the parameter hue. However, if I do that I need to compute ci's separately. Not to mention that it seems a bit of overkill, since the library can already aggregate data and calculate ci's automatically when the parameters are passed in a suitable way.

I know that as a last resort I can use pivot_table to manually aggregate the data and compute the ci's.

So my question boils down to is the hue approach the only way to do it? Or is there a way that will use the columns as per the given data and also compute the ci's on the fly when plotting?

Upvotes: 1

Views: 327

Answers (1)

unixsnob
unixsnob

Reputation: 1705

After much searching the only way to do this is through melt. If anyone wants a more complete answer, please message me and I will add the melt code.

It all boils down to seaborn always wanting the data in single columns. Sometimes it is necessary to explode the data to achieve this.

Upvotes: 1

Related Questions