Free tools Windows power users keep installed
One-click scans. No signup required.
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.
#1 Best Overall
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:
Rank #2
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.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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
labeland calllegend()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 supportsorientation="horizontal".
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.
Quick Recap
Best Value
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.




