How to make horizontal bar chart using seaborn to be stacked to a 100% by hue parameter?

Viewed 4835

I would like to stack the horizontal bars based on the hue parameter. I would like to stack them as a proportion (i.e. 100%).

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame('file.csv')
sns.barplot(x="count", y="new_placement_name", data=df, hue = "index_range" , color="b",ci = None)


    placementname           index_range    Count
0   Great|5/1/2020|||...    High           7
1   Great|5/1/2020|||...    Low            4
2   Great|5/1/2020|||...    Medium         39
3   Great|5/1/2020|||...    Poor           40
4   Great|5/1/2020|||...    Very High      23

output of horizontal bar chart

2 Answers

Seaborn doesn't support stacked bar charts: https://randyzwitch.com/creating-stacked-bar-chart-seaborn/

You can do it with pandas:

df['pct'] = df['Count'] / sum(df['Count'])
df.pivot(index='new_placement_name',columns='Count',
         values='pct').plot(kind='barh',stacked=True,colormap='Blues',rot=90) 

Result:

enter image description here

Yes, you can do it with seaborn. Here, is the link to the documentation : barh in seaborn

Related