Bar Chart Keywords¶
The ax.bar() and ax.barh() methods accept numerous keyword arguments to control bar appearance and behavior.
Width and Height¶
The width parameter controls bar thickness for vertical bars; height for horizontal bars.
1. Default Width¶
import matplotlib.pyplot as plt
import numpy as np
categories = ['A', 'B', 'C', 'D', 'E']
values = [23, 45, 56, 78, 32]
fig, ax = plt.subplots()
ax.bar(categories, values) # Default width = 0.8
plt.show()
2. Custom Width¶
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
for ax, width in zip(axes, [0.3, 0.6, 0.9]):
ax.bar(categories, values, width=width)
ax.set_title(f'width = {width}')
plt.tight_layout()
plt.show()
3. Variable Widths¶
widths = [0.5, 0.7, 0.9, 0.6, 0.4]
fig, ax = plt.subplots()
ax.bar(categories, values, width=widths)
plt.show()
Color¶
The color parameter sets bar fill color.
1. Single Color¶
fig, ax = plt.subplots()
ax.bar(categories, values, color='steelblue')
plt.show()
2. Named Colors¶
ax.bar(categories, values, color='coral')
ax.bar(categories, values, color='forestgreen')
ax.bar(categories, values, color='goldenrod')
3. Individual Colors¶
colors = ['red', 'green', 'blue', 'orange', 'purple']
fig, ax = plt.subplots()
ax.bar(categories, values, color=colors)
plt.show()
Edge Properties¶
Control bar border appearance.
1. Edge Color¶
fig, ax = plt.subplots()
ax.bar(categories, values, color='lightblue', edgecolor='navy')
plt.show()
2. Edge Width¶
ax.bar(categories, values, color='lightblue', edgecolor='navy', linewidth=2)
3. No Edge¶
ax.bar(categories, values, edgecolor='none')
Alpha (Transparency)¶
The alpha parameter sets bar transparency.
1. Uniform Alpha¶
fig, ax = plt.subplots()
ax.bar(categories, values, alpha=0.7)
plt.show()
2. Overlapping Bars¶
x = np.arange(5)
values1 = [23, 45, 56, 78, 32]
values2 = [30, 40, 50, 60, 40]
fig, ax = plt.subplots()
ax.bar(x, values1, alpha=0.7, label='Series 1')
ax.bar(x, values2, alpha=0.7, label='Series 2')
ax.legend()
plt.show()
3. Highlight Effect¶
alphas = [0.3, 0.3, 1.0, 0.3, 0.3] # Highlight third bar
fig, ax = plt.subplots()
for i, (cat, val, a) in enumerate(zip(categories, values, alphas)):
ax.bar(cat, val, alpha=a, color='steelblue')
plt.show()
Alignment¶
The align parameter controls bar position relative to x coordinate.
1. Center Alignment (Default)¶
x = np.arange(5)
fig, ax = plt.subplots()
ax.bar(x, values, align='center')
ax.set_xticks(x)
plt.show()
2. Edge Alignment¶
fig, ax = plt.subplots()
ax.bar(x, values, align='edge')
ax.set_xticks(x)
plt.show()
3. Comparison¶
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
axes[0].bar(x, values, align='center', width=0.8)
axes[0].set_title("align='center'")
axes[0].axvline(x=2, color='red', linestyle='--')
axes[1].bar(x, values, align='edge', width=0.8)
axes[1].set_title("align='edge'")
axes[1].axvline(x=2, color='red', linestyle='--')
plt.tight_layout()
plt.show()
Bottom and Left¶
Offset bar base from zero.
1. Bottom Offset (Vertical)¶
fig, ax = plt.subplots()
ax.bar(categories, values, bottom=10)
ax.axhline(y=10, color='red', linestyle='--')
plt.show()
2. Left Offset (Horizontal)¶
fig, ax = plt.subplots()
ax.barh(categories, values, left=10)
ax.axvline(x=10, color='red', linestyle='--')
plt.show()
3. Building Stacked Bars¶
values1 = [23, 45, 56, 78, 32]
values2 = [15, 25, 20, 30, 18]
fig, ax = plt.subplots()
ax.bar(categories, values1, label='Base')
ax.bar(categories, values2, bottom=values1, label='Addition')
ax.legend()
plt.show()
Error Bars¶
Add error bars with yerr and xerr.
1. Symmetric Error¶
errors = [3, 5, 4, 6, 3]
fig, ax = plt.subplots()
ax.bar(categories, values, yerr=errors, capsize=5)
plt.show()
2. Asymmetric Error¶
lower_errors = [2, 3, 2, 4, 2]
upper_errors = [4, 6, 5, 8, 4]
fig, ax = plt.subplots()
ax.bar(categories, values, yerr=[lower_errors, upper_errors], capsize=5)
plt.show()
3. Error Bar Styling¶
fig, ax = plt.subplots()
ax.bar(categories, values, yerr=errors,
capsize=5,
error_kw={'elinewidth': 2, 'ecolor': 'red', 'capthick': 2})
plt.show()
Label¶
Add labels for legend display.
1. Label Parameter¶
fig, ax = plt.subplots()
ax.bar(categories, values, label='2024 Sales')
ax.legend()
plt.show()
2. Multiple Series¶
x = np.arange(5)
width = 0.35
fig, ax = plt.subplots()
ax.bar(x - width/2, values, width, label='2023')
ax.bar(x + width/2, [v * 1.1 for v in values], width, label='2024')
ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.legend()
plt.show()
3. Legend Location¶
ax.legend(loc='upper left')
ax.legend(loc='upper right')
ax.legend(loc='best')
Hatch Patterns¶
Add patterns to bars for print-friendly distinction.
1. Single Pattern¶
fig, ax = plt.subplots()
ax.bar(categories, values, hatch='//')
plt.show()
2. Available Patterns¶
fig, axes = plt.subplots(2, 4, figsize=(12, 6))
patterns = ['/', '\\', '|', '-', '+', 'x', 'o', 'O']
for ax, pattern in zip(axes.flat, patterns):
ax.bar(['A'], [1], hatch=pattern * 2, edgecolor='black')
ax.set_title(f"hatch='{pattern}'")
plt.tight_layout()
plt.show()
3. Combined with Color¶
fig, ax = plt.subplots()
ax.bar(categories, values, color='lightblue', hatch='//', edgecolor='navy')
plt.show()
Combining Keywords¶
Create styled bar charts with multiple parameters.
1. Professional Style¶
fig, ax = plt.subplots(figsize=(10, 6))
bars = ax.bar(categories, values,
color='steelblue',
edgecolor='navy',
linewidth=1.5,
alpha=0.8,
width=0.6)
ax.bar_label(bars, padding=3)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.show()
2. Conditional Coloring¶
colors = ['green' if v > 50 else 'red' for v in values]
fig, ax = plt.subplots()
ax.bar(categories, values, color=colors, edgecolor='black')
plt.show()
3. Gradient Effect¶
import matplotlib.cm as cm
cmap = cm.get_cmap('Blues')
colors = [cmap(v / max(values)) for v in values]
fig, ax = plt.subplots()
ax.bar(categories, values, color=colors, edgecolor='navy')
plt.show()