Matplotlib errorbar() Function
Matplotlib Reference Documentation
errorbar() Used to draw line plots with error bars, showing the uncertainty range of measured values at each data point.
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
| Parameters | Type | Description |
|---|---|---|
| x, y | array-like | Data point coordinates |
| yerr / xerr | float or array-like | Errors in the y/x direction. A scalar means the same error for all points, an array means independent errors per point. A 2D array can specify lower and upper errors as [lo, hi] |
| fmt | str | Format string for data points, such as '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 caps at error bar ends (in points) |
| capthick | float | Thickness of the caps at error bar ends |
| barsabove | bool | If True, error bars are drawn on top of the data points |
| lolims / uplims | array-like of bool | Mark the lower/upper limit in the y direction (one-sided arrow only) |
| 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 access to the main line and error bar lines separately.
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'
)
Plot a line chart with error bars
ax.
errorbar
(
x
,
y
,
yerr
=
y_err
,
fmt
=
'o-'
,
Circle + line
color
=
'steelblue'
,
ecolor
=
'gray'
,
Error bar color
elinewidth
=
1.5
,
Error bar line width
capsize
=
5
,
Cap 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: Asymmetric 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: the first row is the lower error, the second row is the upper 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 connecting 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: Error bars in both directions (errors in both x and y)
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
(
)
Example 4: errorevery reduces 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: error bars shown at 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: one error bar shown 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
(
)
Common questions
Different shapes of yerr?
Scalar: the same error for all points.
1D array (N,): symmetric error for each point (same upper and lower).
2D array (2, N): the first row is the lower error, the second row is the upper error.
What if you don't want connecting lines between data points?
data to the child component via fmt Set a marker-only format, such as 'o'、's'、'^', omitting the line style portion.