quant
quant

Reputation: 4492

how to order the stacked bar using plotnine

I have the following dataframe

import pandas as pd
from plotnine import *

df = pd.DataFrame({
    'variable': ['gender', 'gender', 'age', 'age', 'age', 'income', 'income', 'income', 'income'],
    'category': ['Female', 'Male', '1-24', '25-54', '55+', 'Lo', 'Lo-Med', 'Med', 'High'],
    'value': [60, 40, 50, 30, 20, 10, 25, 25, 40],
})
df['variable'] = pd.Categorical(df['variable'], categories=['gender', 'age', 'income'])

An I am using the following code to get the stacked bar plot

(ggplot(df, aes(x='variable', y='value', fill='category'))
 + geom_col()
)

The above code was taken from here

How can I change the order in each of the categories. E.g. I want the age 1-24 to be at the bottom of the age stacked bar

Upvotes: 3

Views: 2128

Answers (1)

João Eduardo
João Eduardo

Reputation: 472

You can specify which column order will be stacked by using pd.Categorical function with ordered=True. This attribute ensures the category order will be preserved the way you want during the plotting process:

# Categories are shown in a top-down order, 
# so you must reverse 'ages' order to be shown in bottom-up fashion
cat_order = ['Female', 'Male', '55+', '25-54', '1-24', 'Lo', 'Lo-Med', 'Med', 'High']

df['category'] = pd.Categorical(df['category'], categories=cat_order, ordered=True)

Upvotes: 3

Related Questions