单堆积条形图 Matplotlib

Single Stacked Bar Chart Matplotlib

我正在努力使用 matplotlib 获取单个堆叠条形图。

我想创建这样的东西: Horizontal Stacked Bar Chart

但是,即使我使用 df.plot.barh(stacked=True, ax=axes_var, legend=False),我也会得到两个单独的栏。我的数据框目前看起来像这样:

        Percentage
Female        42.9
Male          57.1

如有任何建议,我们将不胜感激。

首先转置一列DataFrame:

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

如果有 2 列或更多列:

df[['Percentage']].T.plot.barh(stacked=True, legend=False)