Open In App

Create a grouped bar plot in Matplotlib

Last Updated : 09 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A grouped bar plot is a type of bar chart that displays multiple bars for different categories side by side within groups. It is useful for comparing values across multiple dimensions, such as tracking sales across different months for multiple products or analyzing students’ performance in different subjects. By using Matplotlib, we can create grouped bar plots with customization options like colors, labels and spacing to enhance readability and data interpretation.

Steps to Create a Grouped Bar Plot

  1. Import Required Libraries: Load necessary libraries such as Matplotlib and NumPy.
  2. Create or Import Data: Define the dataset to be visualized.
  3. Plot the Bars in a Grouped Manner: Use Matplotlib’s bar() function to generate grouped bars.

Example: In this example, we are creating a basic grouped bar chart to compare two sets of data across five categories.

Python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y1 = [34, 56, 12, 89, 67]
y2 = [12, 56, 78, 45, 90]
width = 0.40

# plot data in grouped manner of bar type
plt.bar(x-0.2, y1, width)
plt.bar(x+0.2, y2, width)

Output

Explanation: x array represents the indices of the bars and y1 and y2 are the values for two different groups. We define a width for the bars and use plt.bar to plot them side by side by shifting their positions (x-0.2 for y1 and x+0.2 for y2), making them appear grouped.

Let’s explore some examples to better understand this.

Example 1: This example demonstrates how to visualize three sets of scores across five teams using a grouped bar chart. Each group (team) contains three bars representing performance in three different rounds.

Python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y1 = [34, 56, 12, 89, 67]
y2 = [12, 56, 78, 45, 90]
y3 = [14, 23, 45, 25, 89]
width = 0.2

# plot data in grouped manner of bar type
plt.bar(x-0.2, y1, width, color='cyan')
plt.bar(x, y2, width, color='orange')
plt.bar(x+0.2, y3, width, color='green')
plt.xticks(x, ['Team A', 'Team B', 'Team C', 'Team D', 'Team E'])
plt.xlabel("Teams")
plt.ylabel("Scores")
plt.legend(["Round 1", "Round 2", "Round 3"])
plt.show()

Output

Explanation: The bars are positioned at x-0.2, x and x+0.2 to ensure they are displayed side by side. We also customize the colors for each group, label the x-axis with team names and add a legend to indicate which bar represents which round.

Example 2: In this example, we are creating a grouped bar chart using Pandas. This method is helpful when the data is already organized in a table format, making it quick and efficient to generate visualizations.

Python
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame([['A', 10, 20, 10, 30], ['B', 20, 25, 15, 25], ['C', 12, 15, 19, 6],
                   ['D', 10, 29, 13, 19]],
                  columns=['Team', 'Round 1', 'Round 2', 'Round 3', 'Round 4'])

# plot grouped bar chart
df.plot(x='Team',
        kind='bar',
        stacked=False,
        title='Grouped Bar Graph with dataframe')

Output

Explanation: The DataFrame stores team names and their scores in multiple rounds. The plot function is used to create a bar chart where the teams are on the x-axis and the scores are plotted as grouped bars.



Next Article
Article Tags :
Practice Tags :

Similar Reads