Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering of elements in Pandas stacked bar chart

I'm trying to graph information about the portion of a household's income earned in a specific industry across 5 districts in a region.

I used groupby to sort the information in my data frame by district:

df = df_orig.groupby('District')['Portion of income'].value_counts(dropna=False)
df = df.groupby('District').transform(lambda x: 100*x/sum(x))
df = df.drop(labels=math.nan, level=1)
ax = df.unstack().plot.bar(stacked=True, rot=0)
ax.set_ylim(ymax=100)

display(df.head())

    District  Portion of income
    A         <25%                 12.121212
              25 - 50%              9.090909
              50 - 75%              7.070707
              75 - 100%             2.020202

Since this income falls into categories, I would like to order the elements in the stacked bar in a logical way. The graph Pandas produced is below. Right now, the ordering (starting from the bottom of each bar) is:

  • 25 - 50%
  • 50 - 75%
  • 75 - 100%
  • <25%
  • Unsure

I realize that these are sorted in alphabetical order and was curious if there was a way to set a custom ordering. To be intuitive, I would like the order to be (again, starting from the bottom of the bar):

  • Unsure
  • <25%
  • 25 - 50%
  • 50 - 75%
  • 75 - 100%

Then, I would like to flip the legend to display the reverse of this order (ie, I would like the legend to have 75 - 100 at the top, as that is what will be at the top of the bars).

like image 325
user2079355 Avatar asked Feb 25 '19 20:02

user2079355


1 Answers

To impose a custom sort order on the income categories, one way is to convert them to a CategoricalIndex.

To reverse the order of matplotlib legend entries, use the get_legend_handles_labels method from this SO question: Reverse legend order pandas plot

import pandas as pd
import numpy as np
import math

np.random.seed(2019)

# Hard-code the custom ordering of categories
categories = ['unsure', '<25%', '25 - 50%', '50 - 75%', '75 - 100%']

# Generate some example data
# I'm not sure if this matches your input exactly
df_orig = pd.DataFrame({'District': pd.np.random.choice(list('ABCDE'), size=100), 
                        'Portion of income': np.random.choice(categories + [np.nan], size=100)})

# Unchanged from your code. Note that value_counts() returns a 
# Series, but you name it df
df = df_orig.groupby('District')['Portion of income'].value_counts(dropna=False)
df = df.groupby('District').transform(lambda x: 100*x/sum(x))

# In my example data, np.nan was cast to the string 'nan', so 
# I have to drop it like this
df = df.drop(labels='nan', level=1)

# Instead of plotting right away, unstack the MultiIndex
# into columns, then convert those columns to a CategoricalIndex 
# with custom sort order
df = df.unstack()

df.columns = pd.CategoricalIndex(df.columns.values, 
                                 ordered=True, 
                                 categories=categories)

# Sort the columns (axis=1) by the new categorical ordering
df = df.sort_index(axis=1)

# Plot
ax = df.plot.bar(stacked=True, rot=0)
ax.set_ylim(ymax=100)

# Matplotlib idiom to reverse legend entries 
handles, labels = ax.get_legend_handles_labels()
ax.legend(reversed(handles), reversed(labels))

Output

like image 97
Peter Leimbigler Avatar answered Nov 15 '22 09:11

Peter Leimbigler