Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Stacked Bar Chart Matplotlib

I am struggling to get a single stacked bar chart using matplotlib.

I want to create something like this: Horizontal Stacked Bar Chart

However, even if I use df.plot.barh(stacked=True, ax=axes_var, legend=False) I get two separate bars. My data frame currently looks like this:

        Percentage
Female        42.9
Male          57.1

Any advice would be appreciated.

like image 314
Lenehan Avatar asked Nov 21 '25 17:11

Lenehan


1 Answers

First transpose one column DataFrame:

df.T.plot.barh(stacked=True, legend=False)

If 2 or more columns:

df[['Percentage']].T.plot.barh(stacked=True, legend=False)
like image 125
jezrael Avatar answered Nov 24 '25 07:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!