Create Multiple Bar Charts in Matplotlib

While working on a project for one of my clients, I’ve found that visualizing data effectively is just as important as analyzing it. One of the most common and efficient ways to compare different categories or groups side by side is through multiple bar charts.

In this tutorial, I’ll walk you through everything you need to know about creating multiple bar charts in Matplotlib. I’ll share practical methods I’ve used in real projects, so you can follow along easily and apply these techniques to your data.

Let’s get started!

What Are Multiple Bar Charts?

Multiple bar charts, often called grouped bar charts, display two or more sets of bars side by side for each category. This format makes it easy to compare values across different groups visually.

For example, imagine you want to compare quarterly sales figures for three different retail stores across the USA. A multiple bar chart lets you see these comparisons clearly in one chart rather than separate ones.

Read Matplotlib Secondary y-Axis

Method 1: Create Grouped Bar Charts with Matplotlib

The easy way to create multiple bar charts is by using Matplotlib’s bar() function with calculated positions for each group of bars.

  1. Prepare your data
    Suppose you have quarterly sales data for three stores: New York, California, and Texas.
import matplotlib.pyplot as plt
import numpy as np

# Sample data
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
store_ny = [25000, 27000, 30000, 28000]
store_ca = [22000, 26000, 31000, 29000]
store_tx = [20000, 24000, 28000, 27000]
  1. Set up bar positions
    We use numpy.arange() to create the positions for each quarter, then offset each store’s bars to avoid overlap.
bar_width = 0.25
x = np.arange(len(quarters))

plt.bar(x - bar_width, store_ny, width=bar_width, label='New York')
plt.bar(x, store_ca, width=bar_width, label='California')
plt.bar(x + bar_width, store_tx, width=bar_width, label='Texas')
  1. Add labels and a title
plt.xlabel('Quarter')
plt.ylabel('Sales ($)')
plt.title('Quarterly Sales Comparison by Store')
plt.xticks(x, quarters)
plt.legend()
plt.show()

You can see the output in the screenshot below.

multiple bar chart matplotlib

This method is flexible and easy to customize. You can add more groups or change colors as needed.

Check out Matplotlib Set Axis Range

Method 2: Use Pandas with Matplotlib for Multiple Bar Charts

If your data is in a pandas DataFrame, plotting grouped bar charts becomes even simpler thanks to pandas’ built-in plotting capabilities.

  1. Create a DataFrame
import pandas as pd

data = {
    'Quarter': quarters,
    'New York': store_ny,
    'California': store_ca,
    'Texas': store_tx
}

df = pd.DataFrame(data)
df.set_index('Quarter', inplace=True)
  1. Plot the chart
df.plot(kind='bar')
plt.title('Quarterly Sales Comparison by Store')
plt.ylabel('Sales ($)')
plt.xlabel('Quarter')
plt.show()

You can see the output in the screenshot below.

matplotlib bar plot multiple columns

This approach is great when your data is already structured in DataFrames. It automatically handles bar grouping and legends.

Read What is the add_axes Matplotlib

Method 3: Horizontal Multiple Bar Charts

Sometimes, horizontal bars are more readable, especially if category names are long.

Here’s how I create horizontal grouped bar charts:

plt.barh(x - bar_width, store_ny, height=bar_width, label='New York')
plt.barh(x, store_ca, height=bar_width, label='California')
plt.barh(x + bar_width, store_tx, height=bar_width, label='Texas')

plt.ylabel('Quarter')
plt.xlabel('Sales ($)')
plt.title('Quarterly Sales Comparison by Store')
plt.yticks(x, quarters)
plt.legend()
plt.show()

You can see the output in the screenshot below.

matplotlib multiple bar chart

Horizontal bars can be particularly helpful when you have many categories or long labels.

Add Labels to Bars for Better Clarity

I often add value labels on top of bars to make the chart more informative. Here’s a quick way:

bars = plt.bar(x, store_ca, width=bar_width, label='California')

for bar in bars:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2, yval + 500, int(yval), ha='center', va='bottom')

This small addition helps viewers quickly grasp exact values without guessing.

Tips for Effective Multiple Bar Charts

  • Limit the number of groups: Too many bars can clutter the chart. Stick to 3-5 groups for clarity.
  • Use contrasting colors: Make each group distinct.
  • Label axes clearly: Always add axis labels and a descriptive title.
  • Consider interactive plots: For complex data, tools like Plotly can enhance user interaction.

Creating multiple bar charts with Matplotlib is a skill that pays off in many data-driven projects. Whether you’re analyzing sales trends, comparing survey results, or tracking performance metrics, these charts provide clear visual insights.

Try these methods with your data, and tweak them to fit your needs. The flexibility of Matplotlib combined with Python’s data handling makes this a powerful combo for any developer or analyst.

Other Python tutorials you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.