Plot Multiple Bar Graphs in Matplotlib with Python

Recently, I was working on a project where I needed to compare sales data of different products across multiple regions in the USA. A simple bar chart wasn’t enough because I wanted to visualize multiple groups side by side.

That’s when I turned to Matplotlib in Python to plot multiple bar graphs. With Python, it’s not only possible but also quite simple once you know the right approach.

In this tutorial, I’ll show you exactly how to plot multiple bar graphs in Matplotlib using Python. I’ll cover different methods, share the full code, and explain each step in a way that even beginners can follow.

Use Multiple Bar Graphs in Python

When you’re analyzing data, sometimes a single bar chart doesn’t give the full picture. Imagine you want to compare the sales of laptops, smartphones, and tablets across different states in the USA.

If you use a single bar chart, you’ll only see one product at a time. But with multiple bar graphs in Python, you can place bars side by side and instantly see how each product performs in every state.

Method 1 – Plot Multiple Bar Graphs Side by Side in Python

The most common way to plot multiple bar graphs in Matplotlib is to place them side by side. This method is useful when you want to compare different categories within the same group.

Here’s an example where I compare sales of three products (Laptops, Smartphones, Tablets)

import matplotlib.pyplot as plt
import numpy as np

# Data
states = ["California", "Texas", "Florida", "New York"]
laptop_sales = [250, 200, 180, 220]
smartphone_sales = [300, 280, 250, 270]
tablet_sales = [150, 130, 120, 160]

# X-axis positions
x = np.arange(len(states))
width = 0.25  # width of each bar

# Plotting multiple bar graphs side by side
plt.bar(x - width, laptop_sales, width, label="Laptops", color="blue")
plt.bar(x, smartphone_sales, width, label="Smartphones", color="green")
plt.bar(x + width, tablet_sales, width, label="Tablets", color="orange")

# Adding labels and title
plt.xlabel("States")
plt.ylabel("Units Sold")
plt.title("Product Sales Comparison Across US States")
plt.xticks(x, states)
plt.legend()

# Display the chart
plt.show()

You can see the output in the screenshot below.

Multiple Bar Graphs in Matplotlib with Python

This Python code creates a grouped bar chart where each state has three bars side by side. One bar shows laptop sales, another shows smartphone sales, and the third shows tablet sales.

Method 2 – Plot Multiple Bar Graphs in a Stacked Format

Sometimes, instead of placing bars side by side, you may want to stack them on top of each other. This is useful when you want to see the total value while still keeping track of individual contributions.

For example, let’s say you want to see the total sales in each state, but also know how much each product contributed.

import matplotlib.pyplot as plt

# Data
states = ["California", "Texas", "Florida", "New York"]
laptop_sales = [250, 200, 180, 220]
smartphone_sales = [300, 280, 250, 270]
tablet_sales = [150, 130, 120, 160]

# Plot stacked bar chart
plt.bar(states, laptop_sales, label="Laptops", color="blue")
plt.bar(states, smartphone_sales, bottom=laptop_sales, label="Smartphones", color="green")
plt.bar(states, tablet_sales, bottom=np.array(laptop_sales) + np.array(smartphone_sales), 
        label="Tablets", color="orange")

# Adding labels and title
plt.xlabel("States")
plt.ylabel("Units Sold")
plt.title("Stacked Product Sales Across US States")
plt.legend()

# Display the chart
plt.show()

You can see the output in the screenshot below.

Plot Multiple Bar Graphs in Python Matplotlib

In this Python example, I used the bottom parameter in Matplotlib to stack the bars. Each product’s sales are placed on top of the previous one, giving a clear picture of both individual and total sales.

Method 3 – Use a For Loop in Python

If you have many categories, writing out each plt.bar() line can get repetitive. A Python for loop makes this easier and more dynamic.

import matplotlib.pyplot as plt
import numpy as np

# Data
states = ["California", "Texas", "Florida", "New York"]
sales_data = {
    "Laptops": [250, 200, 180, 220],
    "Smartphones": [300, 280, 250, 270],
    "Tablets": [150, 130, 120, 160]
}

x = np.arange(len(states))
width = 0.25

# Loop through products and plot
for i, (product, sales) in enumerate(sales_data.items()):
    plt.bar(x + i*width - width, sales, width, label=product)

# Labels and title
plt.xlabel("States")
plt.ylabel("Units Sold")
plt.title("Multiple Bar Graphs Using Python For Loop")
plt.xticks(x, states)
plt.legend()

plt.show()

You can see the output in the screenshot below.

Plot Multiple Bar Graphs in Matplotlib Python

This method is more flexible. If you add another product to the dictionary, it will automatically be included in the bar chart without changing much code.

Method 4 – Plot Multiple Bar Graphs with Custom Colors and Styles

Sometimes, you may want to make your charts more visually appealing by customizing colors, adding gridlines, or changing bar styles.

In Python, Matplotlib allows you to fully control the appearance of your multiple bar graphs.

import matplotlib.pyplot as plt
import numpy as np

# Data
states = ["California", "Texas", "Florida", "New York"]
laptop_sales = [250, 200, 180, 220]
smartphone_sales = [300, 280, 250, 270]
tablet_sales = [150, 130, 120, 160]

x = np.arange(len(states))
width = 0.25

# Custom colors and edge styles
plt.bar(x - width, laptop_sales, width, label="Laptops", color="#1f77b4", edgecolor="black", hatch="/")
plt.bar(x, smartphone_sales, width, label="Smartphones", color="#2ca02c", edgecolor="black", hatch="\\")
plt.bar(x + width, tablet_sales, width, label="Tablets", color="#ff7f0e", edgecolor="black", hatch="-")

# Labels and title
plt.xlabel("States")
plt.ylabel("Units Sold")
plt.title("Customized Multiple Bar Graphs in Python")
plt.xticks(x, states)
plt.legend()
plt.grid(axis="y", linestyle="--", alpha=0.7)

plt.show()

With this Python code, you can create professional-looking bar graphs that stand out in presentations or reports.

Best Practices for Multiple Bar Graphs in Python

  • Keep it simple: Too many categories can make the chart cluttered.
  • Use contrasting colors: Helps distinguish between different bars.
  • Add labels and legends: Always make your chart easy to read.
  • Consider stacked vs grouped: Choose the format that best conveys your message.

Plotting multiple bar graphs in Matplotlib with Python is a powerful way to compare categories across groups.

I showed you how to:

  • Plot grouped bar charts side by side.
  • Create stacked bar charts.
  • Use a Python for loop to automate plotting.
  • Customize colors and styles for better visuals.

I’ve personally used these methods in real-world projects to analyze sales data, compare performance, and present insights to stakeholders. Once you try these out, you’ll see how versatile Python and Matplotlib can be for data visualization.

You may also like to read:

Leave a Comment

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.