How to create stacked bar chart with given dataframe shape?
Question:
I have a dataframe and would like to create a stacked bar chart by having date on the x-axis and quantity on the y-axis. This is the current dataframe:
date | product_group | quantity
2021-10-01 | A | 10
2021-10-01 | C | 10
2021-10-01 | Z | 80
2021-11-11 | A | 13
2021-12-12 | B | 5..
I am trying to get to this output using either matplotlib or seaborn where I have:
- quantity on the x-axis (% stack)
- date on the y-axis
- have quantity stacked for each unique date & product group option. I.e. for date 10-01, we have a stack with A,C,Z and their respective quantities (relative to each other, i.e. A=0.1, C=0.1, Z=0.8)
What is the best approach here? Any advise is appreciated. Thanks
Answers:
It’s a one-liner with histplot
:
sns.histplot(df, y='date', weights='quantity', hue='product_group', multiple='stack')
Edit: if you want all bars to have the same length, set multiple
to fill
:
sns.histplot(df, y='date', weights='quantity', hue='product_group', multiple='fill')
Output:
If you don’t want to use seaborn but rather matplotlib, you could do:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'date': ['2021-10-01']*3 + ['2021-11-11', '2021-11-12'],
'product_group': ['A', 'C', 'Z', 'A', 'B'],
'quantity': [10,10,80,13,5]
})
# pivot values into columns for stacked plot
df = df.pivot(index='date', columns='product_group', values='quantity')
# create percentages
df.div(df.sum(axis=1),axis=0).plot.barh(stacked=True)
I have a dataframe and would like to create a stacked bar chart by having date on the x-axis and quantity on the y-axis. This is the current dataframe:
date | product_group | quantity
2021-10-01 | A | 10
2021-10-01 | C | 10
2021-10-01 | Z | 80
2021-11-11 | A | 13
2021-12-12 | B | 5..
I am trying to get to this output using either matplotlib or seaborn where I have:
- quantity on the x-axis (% stack)
- date on the y-axis
- have quantity stacked for each unique date & product group option. I.e. for date 10-01, we have a stack with A,C,Z and their respective quantities (relative to each other, i.e. A=0.1, C=0.1, Z=0.8)
What is the best approach here? Any advise is appreciated. Thanks
It’s a one-liner with histplot
:
sns.histplot(df, y='date', weights='quantity', hue='product_group', multiple='stack')
Edit: if you want all bars to have the same length, set multiple
to fill
:
sns.histplot(df, y='date', weights='quantity', hue='product_group', multiple='fill')
Output:
If you don’t want to use seaborn but rather matplotlib, you could do:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'date': ['2021-10-01']*3 + ['2021-11-11', '2021-11-12'],
'product_group': ['A', 'C', 'Z', 'A', 'B'],
'quantity': [10,10,80,13,5]
})
# pivot values into columns for stacked plot
df = df.pivot(index='date', columns='product_group', values='quantity')
# create percentages
df.div(df.sum(axis=1),axis=0).plot.barh(stacked=True)