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

Asked By: titutubs

||

Answers:

It’s a one-liner with histplot:

sns.histplot(df, y='date', weights='quantity', hue='product_group', multiple='stack')

Output:
enter image description here

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:

enter image description here

Answered By: Tranbi

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)

enter image description here

Answered By: Lukas Hestermeyer
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.