Matplotlib errorbar() Function
Matplotlib Reference Documentation
errorbar()Used to draw line charts with error bars, displaying the uncertainty range of measured values at the data points.
Widely used in scientific experiments, statistical analysis, and engineering data visualization.
Function Definition
pyplot Interface
matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt='',
ecolor=None, elinewidth=None, capsize=None, barsabove=False,
lolims=False, uplims=False, xlolims=False, xuplims=False,
errorevery=1, capthick=None, *, **kwargs)
Axes Interface
Axes.errorbar(x, y, yerr=None, xerr=None, fmt='', ecolor=None,
elinewidth=None, capsize=None, barsabove=False, lolims=False,
uplims=False, xlolims=False, xuplims=False, errorevery=1,
capthick=None, *, **kwargs)
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| x, y | array-like | Data point coordinates |
| yerr / xerr | float or array-like | Error in the y/x direction. A scalar means the same error for all points; an array means independent errors for each point. A 2D array can specify lower and upper errors as [lo, hi]. |
| fmt | str | Format string for data points, e.g., 'o' (circle), 's' (square), 'o-' (circle + line). |
| ecolor | color | Color of the error bar lines. |
| elinewidth | float | Width of the error bar lines. |
| capsize | float | Length of the error bar cap lines (in points). |
| capthick | float | Thickness of the error bar cap lines. |
| barsabove | bool | If True, error bars are drawn above the data points. |
| lolims / uplims | array-like of bool | Mark the lower/upper limit in the y direction (only one-sided arrows). |
| xlolims / xuplims | array-like of bool | Mark the lower/upper limit in the x direction. |
| errorevery | int | Draw error bars every few data points (reduces visual clutter when data is dense). |
errorbar() returns a
ErrorbarContainerobject, containing(plotline, caplines, barlinecols), allowing separate access to the main line and the error bar lines.
Usage Examples
Example 1: Basic Error Bars
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([3.5, 5.2, 4.8, 6.1, 7.3])
y_err = np.array([0.3, 0.5, 0.4, 0.6, 0.5]) # Error for each point
fig, ax = plt.subplots(layout='constrained')
# Draw a line chart with error bars
ax.errorbar(x, y, yerr=y_err,
fmt='o-', # Circles + line
color='steelblue',
ecolor='gray', # Error bar color
elinewidth=1.5, # Error bar line width
capsize=5, # Cap line length
capthick=1.5,
markersize=8,
label='Measurement')
ax.set_title('Errorbar Plot with y-errors')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([3.5, 5.2, 4.8, 6.1, 7.3])
y_err = np.array([0.3, 0.5, 0.4, 0.6, 0.5]) # Error for each point
fig, ax = plt.subplots(layout='constrained')
# Draw a line chart with error bars
ax.errorbar(x, y, yerr=y_err,
fmt='o-', # Circles + line
color='steelblue',
ecolor='gray', # Error bar color
elinewidth=1.5, # Error bar line width
capsize=5, # Cap line length
capthick=1.5,
markersize=8,
label='Measurement')
ax.set_title('Errorbar Plot with y-errors')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
Example 2: Unequal Errors (Different Upper and Lower)
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = np.array([10, 12, 9, 15, 13])
# 2D array: first row is lower limit error, second row is upper limit error
y_err = np.array([[0.5, 0.8, 0.6, 1.0, 0.7], # Lower error
[1.5, 1.2, 2.0, 0.8, 1.3]]) # Upper error
fig, ax = plt.subplots(layout='constrained')
ax.errorbar(x, y, yerr=y_err,
fmt='s', # Square marker, no line
color='coral',
ecolor='black',
capsize=6,
markersize=10,
markerfacecolor='white',
markeredgewidth=1.5,
label='Asymmetric Error')
ax.set_title('Asymmetric Error Bars (different upper/lower)')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.arange(5)
y = np.array([10, 12, 9, 15, 13])
# 2D array: first row is lower limit error, second row is upper limit error
y_err = np.array([[0.5, 0.8, 0.6, 1.0, 0.7], # Lower error
[1.5, 1.2, 2.0, 0.8, 1.3]]) # Upper error
fig, ax = plt.subplots(layout='constrained')
ax.errorbar(x, y, yerr=y_err,
fmt='s', # Square marker, no line
color='coral',
ecolor='black',
capsize=6,
markersize=10,
markerfacecolor='white',
markeredgewidth=1.5,
label='Asymmetric Error')
ax.set_title('Asymmetric Error Bars (different upper/lower)')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
Example 3: Bidirectional Error Bars (Errors in Both x and y Directions)
Example
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
x = np.arange(8)
y = 2 * x + 1 + np.random.randn(8) * 2
x_err = np.full(8, 0.3) # x-direction error (same for all points)
y_err = np.random.rand(8) * 3 # y-direction error (different for each point)
fig, ax = plt.subplots(figsize=(7, 5), layout='constrained')
ax.errorbar(x, y,
xerr=x_err, # x-direction error
yerr=y_err, # y-direction error
fmt='o',
color='#8e44ad',
ecolor='gray',
capsize=4,
markersize=8,
label='2D Error')
ax.set_title('Error Bars in Both X and Y Directions')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
np.random.seed(42)
x = np.arange(8)
y = 2 * x + 1 + np.random.randn(8) * 2
x_err = np.full(8, 0.3) # x-direction error (same for all points)
y_err = np.random.rand(8) * 3 # y-direction error (different for each point)
fig, ax = plt.subplots(figsize=(7, 5), layout='constrained')
ax.errorbar(x, y,
xerr=x_err, # x-direction error
yerr=y_err, # y-direction error
fmt='o',
color='#8e44ad',
ecolor='gray',
capsize=4,
markersize=8,
label='2D Error')
ax.set_title('Error Bars in Both X and Y Directions')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
Example 4: Using errorevery to Reduce Visual Clutter
Example
import matplotlib.pyplot as plt
import numpy as np
# Dense data points (100 points)
x = np.linspace(0, 10, 100)
y = np.sin(x) + np.random.randn(100) * 0.1
y_err = np.full(100, 0.15)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4),
layout='constrained')
# Left plot: show error bars for every point (too dense)
ax1.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,
capsize=2, elinewidth=0.5, errorevery=1)
ax1.set_title('errorevery=1 (too dense)')
# Right plot: show an error bar every 8 points
ax2.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,
capsize=2, elinewidth=0.5, errorevery=8)
ax2.set_title('errorevery=8 (cleaner)')
for ax in [ax1, ax2]:
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
# Dense data points (100 points)
x = np.linspace(0, 10, 100)
y = np.sin(x) + np.random.randn(100) * 0.1
y_err = np.full(100, 0.15)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4),
layout='constrained')
# Left plot: show error bars for every point (too dense)
ax1.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,
capsize=2, elinewidth=0.5, errorevery=1)
ax1.set_title('errorevery=1 (too dense)')
# Right plot: show an error bar every 8 points
ax2.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,
capsize=2, elinewidth=0.5, errorevery=8)
ax2.set_title('errorevery=8 (cleaner)')
for ax in [ax1, ax2]:
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.grid(True, alpha=0.3)
plt.show()
Frequently Asked Questions
Different Shapes of yerr?
Scalar: same error for all points.
1D array (N,): symmetric error for each point (upper and lower are the same).
2D array (2, N): first row is lower error, second row is upper error.
What to Do If You Don't Want Lines Between Data Points?
willfmtSet it to a marker-only format, e.g.,'o'、's'、'^', omitting the line style part.
Other Extensions