如何命令堆叠的条形图在底部有最大的块? Python

How to order a stacked barplot to have the biggest chunk at the bottom? Python

我正在尝试订购堆叠条形图,其中最大的一块在底部,但我不确定如何订购。

示例数据集

dataset = {'Year': [2019,2019,2020,2020,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2019,2019,2019,2019,2019,2020,
                   2020,2020,2020,2020],
           'Quantity': [100,50,25,30,40,50,200,600,20,40,100,20,50,400,250,300,125,75,90,10,225,550,450,55],
           'Regions': ['Europe','Asia','Africa','Africa','Other','Asia','Africa','Other','America','America','Europe','Europe',
                      'Other','Europe','Asia','Africa','Asia','Europe','Other','Africa','Europe','Asia','Africa','Europe']}
df = pd.DataFrame(data=dataset)

这是我创建图表的代码:

df_pt = df.pivot_table(index='Year', columns='Regions', values='Quantity')

df_pt.plot.bar(stacked=True, figsize=(10, 6))

如果你运行代码你会看到最大的部分不在底部。

输出图:

你是:

from plotnine import ggplot, aes, geom_bar
import pandas as pd

dataset = {'Year': [2019,2019,2020,2020,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2019,2019,2019,2019,2019,2020,
                   2020,2020,2020,2020],
           'Quantity': [100,50,25,30,40,50,200,600,20,40,100,20,50,400,250,300,125,75,90,10,225,550,450,55],
           'Regions': ['Europe','Asia','Africa','Africa','Other','Asia','Africa','Other','America','America','Europe','Europe',
                      'Other','Europe','Asia','Africa','Asia','Europe','Other','Africa','Europe','Asia','Africa','Europe']}
df = pd.DataFrame(data=dataset)
df = df.groupby(['Year', 'Regions']).sum().reset_index()

p = ggplot(df) + aes(x='Year', y='Quantity', group='factor(Quantity)', fill='Regions') + geom_bar(stat='identity', alpha=0.8, show_legend=True)
print(p)