Ricardo Francois
Ricardo Francois

Reputation: 792

How to plot a grouped bar plot of count from pandas

I have a dataframe with the following columns:

gender     class

male       A
female     A
male       B
female     B
male       B
female     A

I want to plot a double bar graph with the columns as each gender and the values as the count of how many of each gender are in class A vs B respectively.

So the bars should be grouped by gender and there should be 2 bars - one for each class.

How do I visualize this? I see this example but I'm really confused

speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant',
         'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan}, index=index)

          speed  lifespan
snail       0.1       2.0
pig        17.5       8.0
elephant   40.0      70.0
rabbit     48.0       1.5
giraffe    52.0      25.0
coyote     69.0      12.0
horse      88.0      28.0

ax = df.plot.bar(rot=0)

enter image description here

My index is just row 0 to the # of rows, so I'm confused how I can configure df.plot.bar to work with my use case. Any help would be appreciated!

Upvotes: 3

Views: 9345

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62523

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = {'gender': ['male', 'female', 'male', 'female', 'male', 'female'], 'class': ['A', 'A', 'B', 'B', 'B', 'A']}
df = pd.DataFrame(data)

# pivot the data and aggregate
dfp = df.pivot_table(index='gender', columns='class', values='class', aggfunc='size')

# plot
dfp.plot(kind='bar', figsize=(5, 3), rot=0)
plt.show()

enter image description here

plt.figure(figsize=(5, 3))
sns.countplot(data=df, x='gender', hue='class')
plt.show()

enter image description here

sns.catplot(kind='count', data=df, x='gender', hue='class', height=3, aspect=1.4)
plt.show()

Upvotes: 3

Related Questions