October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Create Grouped Bar Charts in Matplotlib

Plot multiple datasets side by side in Matplotlib with centered category ticks, reusable bar offsets, and the newer grouped_bar helper.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To plot related datasets side by side for each category, call Axes.bar once per dataset and shift each call’s bar positions around the category center. This works across Matplotlib versions and gives you direct control over bar width, spacing, labels, and ticks. Matplotlib 3.11 also adds a provisional Axes.grouped_bar helper.

Build a grouped bar chart with offset bar calls

Give each category an integer position, then place the bars for each dataset a little to the left or right of that position. The category tick stays at the center of the group.

import numpy as np
import matplotlib.pyplot as plt

categories = ["G1", "G2", "G3"]
series_a = [20, 34, 30]
series_b = [25, 32, 34]

x = np.arange(len(categories))
width = 0.38

fig, ax = plt.subplots()
bars_a = ax.bar(x - width / 2, series_a, width, label="Series A")
bars_b = ax.bar(x + width / 2, series_b, width, label="Series B")
ax.set_xticks(x, categories)
ax.set_ylabel("Value")
ax.legend()
ax.bar_label(bars_a, padding=3)
ax.bar_label(bars_b, padding=3)
fig.tight_layout()
plt.show()

The two calls use the same width and offset their bars by half that width in opposite directions. Each call returns a bar container; passing it to bar_label adds numeric labels. Omit those two calls if labels would make a crowded chart harder to read.

This approach follows Matplotlib’s versioned grouped-bar example. The exact appearance depends on your values and styling; the numbers above are illustrative.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Adapt the offsets for more datasets

For m datasets, center the complete cluster on each category by using this offset for dataset index j:

(j - (m - 1) / 2) * width

For example, with three datasets and width 0.25, the offsets are -0.25, 0, and 0.25. Loop over the datasets and apply the corresponding offset:

datasets = [series_a, series_b, series_c]
names = ["Series A", "Series B", "Series C"]
m = len(datasets)

for j, (values, name) in enumerate(zip(datasets, names)):
    offset = (j - (m - 1) / 2) * width
    ax.bar(x + offset, values, width, label=name)

ax.set_xticks(x, categories)
ax.legend()

Choose a width small enough for the bars in a cluster to sit beside one another without overlapping. Keep the same category positions and width for every dataset; the offset formula centers the cluster rather than placing a dataset’s bars on the category ticks.

Use grouped_bar in Matplotlib 3.11 or newer

Matplotlib’s current stable documentation lists Axes.grouped_bar, added in version 3.11. The API is still provisional, so check your installed Matplotlib version and consider whether relying on a provisional interface suits your project. Unlike separate bar calls, the helper accepts grouped data directly and provides spacing controls.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
fig, ax = plt.subplots(layout="constrained")
result = ax.grouped_bar(
    {"Series A": series_a, "Series B": series_b},
    tick_labels=categories,
)
for container in result.bar_containers:
    ax.bar_label(container, padding=3)
ax.set_ylabel("Value")
ax.legend()
plt.show()

With a dictionary, the keys supply the dataset labels, so do not also pass labels. The helper also supports sequences, 2D arrays, and DataFrames; its options include positions, bar_spacing, group_spacing, colors, and horizontal orientation. See the grouped-bar API reference and the current gallery example.

Check category alignment and choose an orientation

  • Match lengths: Each dataset must have one value for each category. The grouped helper explicitly requires datasets to have the same number of elements; the manual method also needs matching values and category positions for correct alignment.
  • Keep ticks at group centers: Set ticks to x, not to the shifted positions used for an individual dataset’s bars.
  • Label the datasets: Give every bar call a distinct label and call legend() so readers can identify the colors.
  • Use horizontal bars when appropriate: The manual horizontal API is barh, documented in the horizontal-bar reference. The grouped helper supports orientation="horizontal".
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the method that fits your project

Method Version and input Control
Offset bar calls Shown in Matplotlib 3.6.3 documentation; pass each dataset as a separate sequence. Set positions and widths directly.
grouped_bar Added in Matplotlib 3.11; accepts sequences, mappings, 2D arrays, or DataFrames. Offers spacing options, including bar_spacing and group_spacing; the API is provisional.

Use offset calls when you need broad version compatibility or explicit position control. Choose grouped_bar if you are on Matplotlib 3.11 or newer and its more convenient input formats and spacing options fit your chart.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.