I had Facet Grids and managed to make them horizontal, however after that I have searched for an appropriate code to display the percentages as before but what I found just isn't working as expected.
import seaborn as sns
import pandas as pd
import numpy as np
data = {
'id': [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20],
'survey': ['baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline', 'baseline', 'endline'],
'level': ['low', 'high', 'medium', 'low', 'high', 'medium', 'medium', 'high', 'low', 'low', 'medium', 'high', 'low', 'medium', 'low', 'high', 'low', 'low', 'medium', 'high', 'high', 'high', 'high', 'medium', 'low', 'low', 'medium', 'high', 'low', 'medium', 'high', 'medium', 'low', 'high', 'high', 'medium', 'medium', 'low', 'high', 'low']
}
df = pd.DataFrame(data)
df.survey.value_counts()
# Prep data
df = (
df.groupby(["survey", "level"])
.count()
.sort_index(ascending=False)
.rename(columns = {"id": "count"})
)
# Add percentages
pcts = []
for idx in ["endline", "baseline"]:
pcts = (
pcts + (df.loc[(idx,), "count"] * 100 / df.loc[(idx,), "count"].sum()).tolist()
)
df["pct"] = pcts
df = df.reset_index()
df
g = sns.FacetGrid(df, col = "survey")
g.map_dataframe(sns.barplot,'pct', 'level', plt.margins(x = 0.5), hue = 'level', palette = 'Set1', orient = 'h')
g.set_axis_labels('Percentage', 'level')
def annotate_bars(ax=None, fmt='.2f', **kwargs):
ax = plt.gca() if ax is None else ax
total = len(df['pct'])
for p in ax.patches:
percentage = '{:.1f}%'.format(100 * p.get_width()/total)
x = p.get_x() + p.get_width() + 0.02
y = p.get_y() + p.get_height()/2
ax.annotate(percentage, (x, y))
g.map(annotate_bars, fmt='.2g', fontsize=8, color='k')
g.set_titles(col_template = '{col_name} survey')