Skip to content
Advertisement

Order categories in a grouped bar in matplotlib

I am trying to plot a groupby-pandas-dataframe in which I have a categorical variable by which I would like to order the bars.

A sample code of what I am doing:

import pandas as pd

df = {"month":["Jan", "Jan", "Jan","Feb", "Feb", "Mar"], 
      "cat":["High", "High", "Low", "Medium", "Low", "High"]}

df = pd.DataFrame(df)

df.groupby("month")["cat"].value_counts().unstack(0).plot.bar()

Which plots:

enter image description here

However, I would like to plot within each category the order to be Jan, Feb, March.

Any help on how to achieve this would be a appreciated.

Kind regards.

Advertisement

Answer

I recommend you to use the seaborn package for plotting data from dataframes. It’s very simple to organize and order each element when plotting.

First let’s add a column with the counts of each existing month/cat combination:

import pandas as pd

data = {"month":["Jan", "Jan", "Jan","Feb", "Feb", "Mar"], 
      "cat":["High", "High", "Low", "Medium", "Low", "High"]}

df = pd.DataFrame(data)

df = df.value_counts().reset_index().rename(columns={0: 'count'})
print(df)

# output:
# 
#   month     cat  count
# 0   Jan    High      2
# 1   Mar    High      1
# 2   Jan     Low      1
# 3   Feb  Medium      1
# 4   Feb     Low      1

Plotting with seaborn then becomes as simple as:

import matplotlib.pyplot as plt
import seaborn as sns

sns.barplot(
    data=df,
    x='cat',
    y='count', 
    hue='month', 
    order=['Low', 'Medium', 'High'],  # Order of elements in the X-axis 
    hue_order=['Jan', 'Feb', 'Mar'],  # Order of colored bars at each X position
)
plt.show()

Output image:

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement