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
x
9
1
import pandas as pd
2
3
df = {"month":["Jan", "Jan", "Jan","Feb", "Feb", "Mar"],
4
"cat":["High", "High", "Low", "Medium", "Low", "High"]}
5
6
df = pd.DataFrame(df)
7
8
df.groupby("month")["cat"].value_counts().unstack(0).plot.bar()
9
Which plots:
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
1
19
19
1
import pandas as pd
2
3
data = {"month":["Jan", "Jan", "Jan","Feb", "Feb", "Mar"],
4
"cat":["High", "High", "Low", "Medium", "Low", "High"]}
5
6
df = pd.DataFrame(data)
7
8
df = df.value_counts().reset_index().rename(columns={0: 'count'})
9
print(df)
10
11
# output:
12
#
13
# month cat count
14
# 0 Jan High 2
15
# 1 Mar High 1
16
# 2 Jan Low 1
17
# 3 Feb Medium 1
18
# 4 Feb Low 1
19
Plotting with seaborn
then becomes as simple as:
JavaScript
1
13
13
1
import matplotlib.pyplot as plt
2
import seaborn as sns
3
4
sns.barplot(
5
data=df,
6
x='cat',
7
y='count',
8
hue='month',
9
order=['Low', 'Medium', 'High'], # Order of elements in the X-axis
10
hue_order=['Jan', 'Feb', 'Mar'], # Order of colored bars at each X position
11
)
12
plt.show()
13
Output image: