Create a grouped bar plot in Matplotlib
Last Updated :
09 Apr, 2025
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
- Import Required Libraries: Load necessary libraries such as Matplotlib and NumPy.
- Create or Import Data: Define the dataset to be visualized.
- 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.
Similar Reads
Create a stacked bar plot in Matplotlib In this article, we will learn how to Create a stacked bar plot in Matplotlib. Let's discuss some concepts: Matplotlib is a tremendous visualization library in Python for 2D plots of arrays. Matplotlib may be a multi-platform data visualization library built on NumPy arrays and designed to figure wi
3 min read
How to Create Boxplots by Group in Matplotlib? Boxplots by groups can be created using the matplotlib package, but, however, if you wish to make more customizations to your grouped box plot, then the seaborn package provides a go-to function that supports a wide variety of customizations to the grouped box plots. Matplotlib doesn't provide an ex
5 min read
How to create Grouped box plot in Plotly? Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
Bar Plot in Matplotlib A bar plot uses rectangular bars to represent data categories, with bar length or height proportional to their values. It compares discrete categories, with one axis for categories and the other for values.Consider a simple example where we visualize the sales of different fruits:Pythonimport matplo
5 min read
How to Create a Grouped Barplot in R? In this article, we will discuss how to create a grouped barplot in the R programming language. Method 1: Creating A Grouped Barplot In Base R In this method of creating a grouped barplot, the user needs just use the base functionalities of the R language. The user needs to first modify the data use
4 min read