Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to change the space between bars when drawing multiple barplots in Pandas? (Matplotlib)
To change the space between bars when drawing multiple barplots in Pandas, you can use the width parameter in the plot() method to control bar width, or edgecolor and linewidth to create visual separation between bars.
Method 1: Using Width Parameter
The width parameter controls the width of each bar, which indirectly affects the spacing ?
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = {
'Column 1': [i for i in range(5)],
'Column 2': [i * i for i in range(5)]
}
df = pd.DataFrame(data)
# Plot with different bar widths
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
# Default width
df.plot(kind='bar', ax=axes[0], title='Default Width')
# Reduced width (more space between bars)
df.plot(kind='bar', ax=axes[1], width=0.6, title='Width=0.6')
plt.tight_layout()
plt.show()
Method 2: Using EdgeColor and LineWidth
Adding white edges with linewidth creates visual separation between grouped bars ?
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Sales Q1': [100, 150, 200, 120, 180],
'Sales Q2': [120, 160, 190, 140, 200],
'Sales Q3': [110, 140, 210, 130, 170]
}
df = pd.DataFrame(data, index=['Jan', 'Feb', 'Mar', 'Apr', 'May'])
# Plot with edge styling for separation
df.plot(kind='bar',
edgecolor='white',
linewidth=2,
figsize=(10, 6),
width=0.8)
plt.title('Quarterly Sales Data')
plt.ylabel('Sales Amount')
plt.legend(loc='upper left')
plt.xticks(rotation=45)
plt.show()
Method 3: Direct Matplotlib for Custom Spacing
For more control over spacing, use matplotlib directly with custom positioning ?
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = {
'Product A': [25, 30, 35, 20],
'Product B': [20, 25, 30, 25],
'Product C': [15, 20, 25, 30]
}
df = pd.DataFrame(data, index=['Q1', 'Q2', 'Q3', 'Q4'])
# Custom bar positioning
x = np.arange(len(df.index))
width = 0.25 # Width of bars
spacing = 0.05 # Additional spacing
fig, ax = plt.subplots(figsize=(10, 6))
# Plot each column with custom positioning
for i, column in enumerate(df.columns):
offset = (i - 1) * (width + spacing)
ax.bar(x + offset, df[column], width, label=column)
ax.set_xlabel('Quarter')
ax.set_ylabel('Sales')
ax.set_title('Product Sales by Quarter')
ax.set_xticks(x)
ax.set_xticklabels(df.index)
ax.legend()
plt.show()
Comparison
| Method | Control Level | Best For |
|---|---|---|
width parameter |
Basic | Quick adjustment of bar thickness |
edgecolor + linewidth |
Medium | Visual separation without changing layout |
| Direct matplotlib | Full | Custom spacing and complex layouts |
Conclusion
Use the width parameter for quick bar width adjustments, edgecolor with linewidth for visual separation, or direct matplotlib for complete control over bar spacing and positioning.
Advertisements
