'Stacked and grouped barplot - python [duplicate]
I've got a dataset that looks like the following:
| Category | clicked | Case1 | Case2 | Case3 |
|---|---|---|---|---|
| A | 0 | 0.94 | 0.95 | 0.95 |
| A | 1 | 0.06 | 0.05 | 0.05 |
| B | 0 | 0.93 | 0.96 | 0.95 |
| B | 1 | 0.07 | 0.04 | 0.05 |
| C | 0 | 0.93 | 0.96 | 0.95 |
| C | 1 | 0.07 | 0.04 | 0.05 |
| D | 0 | 0.94 | 0.96 | 0.95 |
| D | 1 | 0.06 | 0.04 | 0.05 |
| E | 0 | 0.92 | 0.96 | 0.95 |
| E | 1 | 0.08 | 0.04 | 0.05 |
| F | 0 | 0.93 | 0.95 | 0.95 |
| F | 1 | 0.07 | 0.05 | 0.05 |
| G | 0 | 0.90 | 0.95 | 0.95 |
| G | 1 | 0.10 | 0.05 | 0.05 |
| H | 0 | 0.93 | 0.94 | 0.94 |
| H | 1 | 0.07 | 0.06 | 0.06 |
| X | 0 | 0.71 | 0.95 | 0.95 |
| X | 1 | 0.29 | 0.05 | 0.05 |
From this I'd like to create a stacked and grouped barpot (using seaborn if possible as the rest of my plots are made using just that) that would be something like this:
(taken from This answer of the same question using R)
Translating it into my data, 1,2,3,4,etc would be Grade, R-W (i.e. the grouped part) would be Case1, Case2, Case3, and A-B (i.e the stacked part) would be clicked.
The closest I got was using:
df1=pd.melt(df, id_vars=['Grade'],value_vars=['Case1','Case2','Case3'])
sns.barplot(data=df1, x='Grade', y='value', hue='variable', dodge=True)
but it doesn't include the stacked/clicked part.
Or:
sns.barplot(data=df,
x='Grade', y='Case1', hue='clicked', dodge=True)
This includes the clicked part but is not stacked and couldn't figure out how to add the other "case" variables.
Is there a straightforward-ish way to achieve this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

