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)
Answers:
- Convert the dataframe to a long form with
panda.DataFrame.melt
, then do one of the following:
- Use
pandas.DataFrame.pivot_table
to reshape and aggregate size
, and then plot with pandas.DataFrame.plot
- Plot directly with
seaborn.countplot
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()
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()
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)
- Convert the dataframe to a long form with
panda.DataFrame.melt
, then do one of the following:- Use
pandas.DataFrame.pivot_table
to reshape and aggregatesize
, and then plot withpandas.DataFrame.plot
- Plot directly with
seaborn.countplot
- Use
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()
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()