0

I want to create barplot with seaborn that will display the mean value for three columns from pandas dataframe My dataframe is ismilar to this:

>>>name      treatment    weight    height   ind1
0  Miki      93A control  77.5      23.2     105.5
1  Lorense   75B high     35.1      25.1     57.3
2  Derek     93A high     74.3      26.4     94.5
3  Lily      24C medium   12.2      14.4     26.8
4  Tim       75B high     37.2      26.4     55.1
...

I want to get bar plot that has the treatments as x -axis, and the weight, height and ind1 as the bars. and on this to display the error.

I have tried to do it simply with seaborn, but it works to me only when I try to plot on variable, such as weight, hright or ind1, but does not let me add more than one "variable" :

ax = sns.barplot(x="treatments", y="weight", data=df)

(illustration of the results:): enter image description here

I have tried to create list of variables like in matplotlib for the y :

ax = sns.barplot(x="Treat Name alias", y=["weight","height","ind1"], data=df)

but then I get the error messege:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

the problem is that I would like to have more than one columns as the y value. I have seen the catplot option but it seems that they grouping specific columns and not plotting each column as y value with the same x-axis.

My desired output will be to have the treatment as x-axis, and then for eac treatment to have three bars, one for weight,one for height and one for the ind1. And to have the std plot (the "black line" on the bars).

Reut
  • 1,555
  • 4
  • 23
  • 55
  • @anky that creates hige chart with long x axis that has the treatments repeating many times and no error appears. I would like to use te seaborn package in order to have the error easily. – Reut Feb 02 '21 at 10:19
  • got it: [seaborn multiple variables group bar plot](https://stackoverflow.com/questions/49505456/seaborn-multiple-variables-group-bar-plot) this has been answered here, if you need something else, let me know and edit the question. For your question. All of the solution works. – anky Feb 02 '21 at 10:23
  • bdw i actually meant this: `df.groupby("treatment",sort=False).mean().plot.bar()` :) didn't see the std requirement sorry. – anky Feb 02 '21 at 10:35

1 Answers1

2

First you'll need to reshape your dataframe

df = df.melt(id_vars=['name', 'treatment'], var_name='measurement', value_name='mean')

Resulting in...

   name treatment measurement   mean
0   93A   control      weight   77.5
1   75B      high      weight   35.1
2   93A      high      weight   74.3
3   24C    medium      weight   12.2
4   75B      high      weight   37.2
5   93A   control      height   23.2
6   75B      high      height   25.1
7   93A      high      height   26.4
8   24C    medium      height   14.4
9   75B      high      height   26.4
10  93A   control        ind1  105.5
11  75B      high        ind1   57.3
12  93A      high        ind1   94.5
13  24C    medium        ind1   26.8
14  75B      high        ind1   55.1

Now when you plot it in seaborn you'll have multiple variables 'within' one column, so you can call it like this...

g = sns.catplot(
    data=df, kind="bar",
    x="treatment", y="mean", hue="measurement",
    ci="sd", palette='colorblind', alpha=.6, height=6
)

This gives you...

example result

mullinscr
  • 1,668
  • 1
  • 6
  • 14