How to create a grouped bar plot of categorical counts

Question:

This might be a simple task but I am new to plotting in Python and I struggle to convert logic into code. I’m using the code below but I would like to separate the orange and blue lines (not superimposed). I need to create a horizontal bar plot with the 2 bars separated?

df = pd.DataFrame({'a':[1,2,3,1,2,2,2],
             'b':[1,1,1,3,2,2,2]})
ax = df['a'].value_counts().plot(kind='barh', color='skyblue', width=.75, legend=True, alpha=0.8)
df['b'].value_counts().plot(kind='barh', color='orange', width=.5, alpha=1, legend=True)

enter image description here

Asked By: fredooms

||

Answers:

Try this:

df['a'].value_counts().to_frame('a').join(df['b'].value_counts().to_frame('b')).plot(kind='barh')

enter image description here

Answered By: Reza
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'a':[1,2,3,1,2,2,2], 'b':[1,1,1,3,2,2,2]})

# convert to long form
df = df.melt()

# pivot_table and aggregate
df = df.pivot_table(index='value', columns='variable', values='value', aggfunc='size')

# plot
df.plot(kind='barh', figsize=(4, 3))
ax.legend(bbox_to_anchor=(1, 1.02), loc='upper left')
plt.show()

enter image description here

seaborn.countplot

df = pd.DataFrame({'a':[1,2,3,1,2,2,2], 'b':[1,1,1,3,2,2,2]})

plt.figure(figsize=(4, 3))
ax = sns.countplot(data=df.melt(), y='value', hue='variable')
sns.move_legend(ax, bbox_to_anchor=(1, 1.02), loc='upper left')
plt.show()

enter image description here

Answered By: Trenton McKinney