Home > front end >  How to create a stacked bar chart with a series in matplotlib or seaborn?
How to create a stacked bar chart with a series in matplotlib or seaborn?

Time:09-23

It sounds simple but I cant get a stacked bar chart. I got a series after grouping by a column in a dataframe.

s1 = df_sort.groupby('loan_status')['count_loans'].sum()
s1

loan_status
Fully paid        8045
Not fully paid    1533
Name: count_loans, dtype: int64

I have tried

s1.T.plot.bar(stacked=True)

s1.plot(kind="bar", stacked=True)

which produces two individual bars?

thanks.

CodePudding user response:

You should change the groupby line to:

df = df.groupby(by = 'loan_status').sum().T

Then you can plot with:

df.plot(kind = 'bar', stacked = True)

plt.show()

enter image description here

CodePudding user response:

You have to convert your Series to a DataFrame else you can't transpose:

s1.to_frame().T.plot(kind='bar', stacked=True, rot=0)
plt.show()

stackbar

  • Related