Stacked bar chart: How to change order of values/categories?

Issue

Using a public dataset:

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/flights.csv")
dfplot = df.groupby(["year","month"])["passengers"].sum().reset_index(name ='passengers')
dfplot['passengers_pct'] = dfplot['passengers'] / dfplot.groupby(["year"])['passengers'].transform('sum')
dfplot = dfplot.set_index('year')

dfplot.set_index('month',append=True)['passengers_pct'].unstack().plot.bar(stacked=True,figsize=(10,8))

I get:

stacked bar chart (please click)

…how can I change the order of each year’s stacked bars to a custom defined list? E.g.:
categories = ['January','February','March','April','May','June','July','August','September','October','November','December']

Thanks 🙂

Solution

Can you try the following:

df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/flights.csv")
dfplot = df.groupby(["year","month"])["passengers"].sum().reset_index(name ='passengers')
dfplot['passengers_pct'] = dfplot['passengers'] / dfplot.groupby(["year"])['passengers'].transform('sum')
dfplot = dfplot.set_index('year')

categories = ['January','February','March','April','May','June','July','August','September','October','November','December']
dfplot.month = pd.CategoricalIndex(dfplot.month.values, 
                                 ordered=True, 
                                 categories=categories)
dfplot = dfplot.sort_values(by=['month'])

dfplot.set_index('month',append=True)['passengers_pct'].unstack().plot.bar(stacked=True,figsize=(10,8))

Answered By – Jeril

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published