Using pandas crosstab to create a bar plot

  • As you said you can use pandas to create the stacked bar plot. The argument that you want to have a “seaborn plot” is irrelevant, since every seaborn plot and every pandas plot are in the end simply matplotlib objects, as the plotting tools of both libraries are merely matplotlib wrappers.
  • Here’s a complete solution (using the data creation from @andrew_reece’s answer).
  • Tested in python 3.8.11, pandas 1.3.2, matplotlib 3.4.3, seaborn 0.11.2
import numpy as np 
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

n = 500
np.random.seed(365)
mark = np.random.choice([True, False], n)
periods = np.random.choice(['BASELINE', 'WEEK 12', 'WEEK 24', 'WEEK 4'], n)

df = pd.DataFrame({'mark': mark, 'period': periods})
ct = pd.crosstab(df.period, df.mark)
    
ax = ct.plot(kind='bar', stacked=True, rot=0)
ax.legend(title="mark", bbox_to_anchor=(1, 1.02), loc="upper left")

# add annotations if desired
for c in ax.containers:
    
    # set the bar label
    ax.bar_label(c, label_type="center")

enter image description here

Leave a Comment