How to increase the thickness of error line in a Matplotlib bar chart?

To increase the thickness of error lines in a Matplotlib bar chart, we can use the error_kw parameter with a dictionary containing line width properties. This parameter controls the appearance of error bars including their thickness, cap size, and cap thickness.

Basic Bar Chart with Default Error Lines

Let's first create a simple bar chart with default error lines ?

import matplotlib.pyplot as plt

# Sample data
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
values = [20, 35, 30, 35, 27]
errors = [2, 3, 4, 1, 2]

fig, ax = plt.subplots(figsize=(7.5, 3.5))

# Bar chart with default error lines
ax.bar(labels, values, 0.35, yerr=errors, label='Default')

plt.title('Bar Chart with Default Error Lines')
plt.show()

Increasing Error Line Thickness

Use the error_kw parameter to customize error line appearance. The lw (linewidth) controls the thickness of error lines ?

import matplotlib.pyplot as plt

# Sample data
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
values = [20, 35, 30, 35, 27]
errors = [2, 3, 4, 1, 2]

fig, ax = plt.subplots(figsize=(7.5, 3.5))

# Bar chart with thick error lines
ax.bar(labels, values, 0.35, 
       yerr=errors, 
       label='Thick Error Lines',
       error_kw=dict(lw=5, capsize=5, capthick=3))

plt.title('Bar Chart with Thick Error Lines')
plt.legend()
plt.show()

Error Line Parameters

Parameter Description Example Value
lw Line width of error bars 5
capsize Length of the error bar caps 5
capthick Thickness of the error bar caps 3

Comparison of Different Thicknesses

Here's a side−by−side comparison of different error line thicknesses ?

import matplotlib.pyplot as plt

# Sample data
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
values = [20, 35, 30, 35, 27]
errors = [2, 3, 4, 1, 2]

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 4))

# Default thickness
ax1.bar(labels, values, 0.35, yerr=errors, color='lightblue')
ax1.set_title('Default (lw=1)')

# Medium thickness
ax2.bar(labels, values, 0.35, yerr=errors, color='lightgreen',
        error_kw=dict(lw=3, capsize=3, capthick=2))
ax2.set_title('Medium (lw=3)')

# Thick error lines
ax3.bar(labels, values, 0.35, yerr=errors, color='lightcoral',
        error_kw=dict(lw=6, capsize=6, capthick=4))
ax3.set_title('Thick (lw=6)')

plt.tight_layout()
plt.show()

Conclusion

Use the error_kw parameter with lw to control error line thickness in Matplotlib bar charts. Combine with capsize and capthick for complete error bar customization.

Updated on: 2026-03-25T23:12:30+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements