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:

JavaScript

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:

JavaScript

Plotting with seaborn then becomes as simple as:

JavaScript

Output image:

enter image description here

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