Sunday 13 December 2020

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

I am trying to have my stacked barplot ordered with the largest piece at the bottom but I am not sure how to order it.

Sample dataset

dataset = {'Year': [2019,2020,2020,2019,2019,2020,2017,2017,2018,2020,2018,2016],
           'Quantity': [100,50,25,30,40,50,200,600,20,40,100,20],
           'Regions': ['Europe','Asia','Africa','Africa','Other','Asia','Africa','Other','America','America','Europe','Europe']}
df = pd.DataFrame(data=dataset)

Here is my code to create the graph:

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

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

If you run the code you will see that the biggest part is not at the bottom.

An expected output would be something like that: enter image description here



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

No comments:

Post a Comment