Setting the spacing between grouped bar plots in Matplotlib

Setting the spacing between grouped bar plots in Matplotlib allows you to control how bars are positioned relative to each other. This is useful for creating visually appealing charts with proper separation between grouped data.

Understanding Bar Plot Spacing

The key parameter for controlling spacing is the width parameter in the plot() method. Smaller width values create more spacing between bars, while larger values reduce the spacing.

Basic Example

Let's create a grouped bar plot with custom spacing ?

import matplotlib.pyplot as plt
import pandas as pd

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample data
data = {"Name": ["John", "Jacks", "James", "Joe"],
        "Age": [23, 12, 30, 26],
        "Marks": [98, 85, 70, 77]}

df = pd.DataFrame(data)

# Plot with narrow bars (more spacing)
df.set_index('Name').plot(kind="bar", align='center', width=0.1)
plt.tick_params(rotation=45)
plt.title("Grouped Bar Plot with Narrow Bars (width=0.1)")

plt.show()

Comparing Different Width Values

Let's see how different width values affect the spacing ?

import matplotlib.pyplot as plt
import pandas as pd

# Sample data
data = {"Name": ["John", "Jacks", "James", "Joe"],
        "Age": [23, 12, 30, 26],
        "Marks": [98, 85, 70, 77]}

df = pd.DataFrame(data)

# Create subplots to compare different widths
fig, axes = plt.subplots(1, 3, figsize=(15, 4))

# Width = 0.1 (narrow bars, more spacing)
df.set_index('Name').plot(kind="bar", ax=axes[0], width=0.1, title="Width = 0.1")
axes[0].tick_params(rotation=45)

# Width = 0.5 (medium bars)
df.set_index('Name').plot(kind="bar", ax=axes[1], width=0.5, title="Width = 0.5")
axes[1].tick_params(rotation=45)

# Width = 0.8 (wide bars, less spacing)
df.set_index('Name').plot(kind="bar", ax=axes[2], width=0.8, title="Width = 0.8")
axes[2].tick_params(rotation=45)

plt.tight_layout()
plt.show()

Manual Bar Positioning with Matplotlib

For more precise control over spacing, you can use matplotlib's bar() function directly ?

import matplotlib.pyplot as plt
import numpy as np

# Sample data
names = ['John', 'Jacks', 'James', 'Joe']
ages = [23, 12, 30, 26]
marks = [98, 85, 70, 77]

# Set positions for bars
x = np.arange(len(names))
bar_width = 0.35
spacing = 0.1  # Additional spacing between groups

# Create bars with custom spacing
plt.figure(figsize=(10, 6))
plt.bar(x - bar_width/2 - spacing/2, ages, bar_width, label='Age', alpha=0.8)
plt.bar(x + bar_width/2 + spacing/2, marks, bar_width, label='Marks', alpha=0.8)

# Customize the plot
plt.xlabel('Names')
plt.ylabel('Values')
plt.title('Grouped Bar Plot with Custom Spacing')
plt.xticks(x, names)
plt.legend()
plt.grid(True, alpha=0.3)

plt.show()

Width Parameter Guidelines

Width Value Spacing Effect Best Use Case
0.1 - 0.3 Very wide spacing Few data points, emphasis on separation
0.4 - 0.6 Moderate spacing Balanced appearance
0.7 - 0.9 Minimal spacing Many data points, space-efficient

Conclusion

Control bar spacing using the width parameter in pandas plot() method. Smaller width values create more spacing, while larger values reduce spacing between grouped bars.

---
Updated on: 2026-03-25T21:50:07+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements