0
votes

I've read the several threads about plotting grouped Seaborn Boxplots, but I was wondering, if there is a simpler solution as a one-liner?

The Pandas Dataframe contains something along the lines of:

Index xaxis yaxis1 xayis2
0     A     30     1985
1     A     29     2002
2     B     21     3034
3     A     31     2087
4     B     19     2931
5     B     21     2832
6     A     28     1950

sns.boxplot(x='xaxis', y=['yaxis1','yaxis2'], data=df);

doesn't work (for probably obvious reasons), while

sns.boxplot(x='xaxis', y='yaxis1', data=df);

or

sns.boxplot(x='xaxis', y='yaxis2', data=df);

work just fine for the separate plots. I also tried using

sns.boxplot(df['xaxis'], df[['yaxis1','yaxis2']])

but no luck therewith either...

I want both yaxis columns combined into a single boxplot, similar to this one https://seaborn.pydata.org/examples/grouped_boxplot.html, but I can't use hue=, as the data for both y axes is continuous.

Any way I can do that with the one line sprint, or is it inevitable to run the whole marathon?

1
Welcome to Stack Overflow! Please take a moment to read How do I ask a good question?. You need to provide a Minimal, Complete, and Verifiable example that includes a toy dataset (refer to How to make good reproducible pandas examples)Diziet Asahi
@DizietAsahi fair enough, I'll give it another shot on Friday!Frédéric Jung-Plan
sns.boxplot(data=df[['xaxis', 'yaxis1', 'yaxis2']]) should work, but it is unclear what your are expecting.JohanC

1 Answers

0
votes

If you want to create grouped boxplots with seaborn, you have to use hue=. The trick is to create a long-form dataframe where all your yaxis{1,2} values are in one column, and an other column instructs which of the two original columns each row comes from.

This is accomplished using DataFrame.melt():

df
|   Index | xaxis   |   yaxis1 |   xayis2 |
|--------:|:--------|---------:|---------:|
|       0 | A       |       30 |     1985 |
|       1 | A       |       29 |     2002 |
|       2 | B       |       21 |     3034 |
|       3 | A       |       31 |     2087 |
|       4 | B       |       19 |     2931 |
|       5 | B       |       21 |     2832 |
|       6 | A       |       28 |     1950 |
df2 = df.melt(id_vars=['xaxis'], var_name='yaxis')
|    | xaxis   | yaxis   |   value |
|---:|:--------|:--------|--------:|
|  0 | A       | yaxis1  |      30 |
|  1 | A       | yaxis1  |      29 |
|  2 | B       | yaxis1  |      21 |
|  3 | A       | yaxis1  |      31 |
|  4 | B       | yaxis1  |      19 |
|  5 | B       | yaxis1  |      21 |
|  6 | A       | yaxis1  |      28 |
|  7 | A       | xayis2  |    1985 |
|  8 | A       | xayis2  |    2002 |
|  9 | B       | xayis2  |    3034 |
| 10 | A       | xayis2  |    2087 |
| 11 | B       | xayis2  |    2931 |
| 12 | B       | xayis2  |    2832 |
| 13 | A       | xayis2  |    1950 |
sns.boxplot(x='xaxis', y='value', hue='yaxis', data=df2)

enter image description here