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

Time:06-11

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:

enter image description here

  • Related