How to Create a Grouped Bar Chart in Matplotlib

Spread the love

In this post, you will learn how to create a Grouped Bar Chart in Matplotlib. –

Let’s read a dataset to illustrate it.

import pandas as pd
url = ""
df = pd.read_csv(url)

Now, let’s prepare the data before we do any kind of plotting.

df = df[df['Method of Payment']!= "American Express"]
pay_gender = df.groupby(['Method of Payment','Gender'])['Net Sales'].sum().reset_index()

Here, I first removed the American Express data because the male data is missing. Then I grouped the data by Method of Payment and Gender.

Now, let’s create the Grouped Bar Chart

import matplotlib.pyplot as plt
import numpy as np

female = pay_gender[pay_gender['Gender'] == 'Female']
male = pay_gender[pay_gender['Gender'] == 'Male']

x_pos = np.arange(len(male))
tick_labels = ['Discover','MasterCard','Proprietary Card','Visa']

plt.figure(figsize=(10, 8)) - 0.2, female['Net Sales'], width=0.4, label='Female') + 0.2, male['Net Sales'], width=0.4, label='Male')
plt.xticks(x_pos, tick_labels)

To keep the code clean and readable, I separated the male and female data. The x_pos tells matplotlib the locations of the bars on the x axis. Then we created two bars charts. The position of the bars for the first one is x_pos – 0.2 and the second one is x_pos + 0.2. Doing this will help us show bars next to each others. I also used the width parameter so that bars does not overlap on each other. plt.xticks() changes the x_pos with the tick labels so that we can understand which bar belongs to which category.

Rating: 1 out of 5.

Leave a Reply