YouTip LogoYouTip

Matplotlib Ref Errorbar

Matplotlib errorbar() Function |

\\n\\n

Image 1: Matplotlib Reference Documentation Matplotlib Reference Documentation

\\n\\n

The errorbar() function is used to plot line charts with error bars, displaying the uncertainty range of measured values above each data point.

\\n\\n

Widely used in scientific experiments, statistical analysis, and engineering data visualization.

\\n\\n

Function Definition

\\n\\n

pyplot Interface

\\n

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)

\\n\\n

Axes Interface

\\n

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)

\\n\\n

Parameter Description

\\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n
ParameterTypeDescription
x, yarray-likeCoordinates of data points
yerr / xerrfloat or array-likeError in y/x direction. A scalar indicates the same error for all points; an array indicates independent error per point. A 2D array can specify [low, high] errors separately.
fmtstrFormat string for data points, e.g., 'o' (circle), 's' (square), 'o-' (circle + line)
ecolorcolorColor of the error bar lines
elinewidthfloatWidth of the error bar lines
capsizefloatLength of the horizontal caps at error bar ends (in points)
capthickfloatThickness of the horizontal caps at error bar ends
barsaboveboolIf True, error bars are drawn above the data points
lolims / uplimsarray-like of boolMark lower/upper bounds in y-direction (only single-sided arrows)
xlolims / xuplimsarray-like of boolMark lower/upper bounds in x-direction
erroreveryintDraw error bars every N data points (to reduce visual clutter for dense data)
\\n\\n
\\n

errorbar() returns an ErrorbarContainer object containing (plotline, caplines, barlinecols), allowing separate access to the main line and error bar lines.

\\n
\\n\\n
\\n\\n

Usage Examples

\\n\\n

Example 1: Basic Error Bars

\\n\\n

Examples

\\n\\n
import matplotlib.pyplot as plt\\nimport numpy as np\\n\\nx = np.array([1,2,3,4,5])\\ny = np.array([3.5,5.2,4.8,6.1,7.3])\\ny_err = np.array([0.3,0.5,0.4,0.6,0.5])  # Error Per Point\\n\\nfig, ax = plt.subplots(layout='constrained')\\n\\n# Plot Line Chart with Error Bars\\nax.errorbar(x, y, yerr=y_err,\\n            fmt='o-',  # Circle Markers + Lines\\n            color='steelblue',\\n            ecolor='gray',  # Error Bar Color\\n            elinewidth=1.5,  # Error Bar Line Width\\n            capsize=5,  # Cap Size\\n            capthick=1.5,\\n            markersize=8,\\n            label='Measurement')\\n\\nax.set_title('Errorbar Plot with y-errors')\\nax.set_xlabel('X')\\nax.set_ylabel('Y')\\nax.legend()\\nax.grid(True, alpha=0.3)\\n\\nplt.show()\\n
\\n\\n

Example 2: Asymmetric Errors (Different Upper/Lower)

\\n\\n

Examples

\\n\\n
import matplotlib.pyplot as plt\\nimport numpy as np\\n\\nx = np.arange(5)\\ny = np.array([10,12,9,15,13])\\n\\n# 2D Array: First Row for Lower Error, Second Row for Upper Error\\ny_err = np.array([[0.5,0.8,0.6,1.0,0.7],  # Lower Error\\n                  [1.5,1.2,2.0,0.8,1.3]])  # Upper Error\\n\\nfig, ax = plt.subplots(layout='constrained')\\n\\nax.errorbar(x, y, yerr=y_err,\\n            fmt='s',  # Square Markers, No Lines\\n            color='coral',\\n            ecolor='black',\\n            capsize=6,\\n            markersize=10,\\n            markerfacecolor='white',\\n            markeredgewidth=1.5,\\n            label='Asymmetric Error')\\n\\nax.set_title('Asymmetric Error Bars (different upper/lower)')\\nax.set_xlabel('X')\\nax.set_ylabel('Y')\\nax.legend()\\nax.grid(True, alpha=0.3)\\n\\nplt.show()\\n
\\n\\n

Example 3: Bidirectional Error Bars (Both X and Y Directions)

\\n\\n

Examples

\\n\\n
import matplotlib.pyplot as plt\\nimport numpy as np\\n\\nnp.random.seed(42)\\nx = np.arange(8)\\ny = 2 * x + 1 + np.random.randn(8) * 2\\nx_err = np.full(8, 0.3)  # x Directional Error(same for all points)\\ny_err = np.random.rand(8) * 3  # y Directional Error(different for each point)\\n\\nfig, ax = plt.subplots(figsize=(7,5), layout='constrained')\\n\\nax.errorbar(x, y,\\n            xerr=x_err,  # x Directional Error\\n            yerr=y_err,  # y Directional Error\\n            fmt='o',\\n            color='#8e44ad',\\n            ecolor='gray',\\n            capsize=4,\\n            markersize=8,\\n            label='2D Error')\\n\\nax.set_title('Error Bars in Both X and Y Directions')\\nax.set_xlabel('X')\\nax.set_ylabel('Y')\\nax.legend()\\nax.grid(True, alpha=0.3)\\n\\nplt.show()\\n
\\n\\n

Example 4: Using errorevery to Reduce Visual Clutter

\\n\\n

Examples

\\n\\n
import matplotlib.pyplot as plt\\nimport numpy as np\\n\\n# Dense Data Points (100 Points)\\nx = np.linspace(0, 10, 100)\\ny = np.sin(x) + np.random.randn(100) * 0.1\\ny_err = np.full(100, 0.15)\\n\\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4),\\n                               layout='constrained')\\n\\n# Left Plot: Error Bar on Every Point (Too Dense)\\nax1.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,\\n             capsize=2, elinewidth=0.5, errorevery=1)\\nax1.set_title('errorevery=1 (too dense)')\\n\\n# Right Plot: Error Bar Every 8 Points\\nax2.errorbar(x, y, yerr=y_err, fmt='o', markersize=3,\\n             capsize=2, elinewidth=0.5, errorevery=8)\\nax2.set_title('errorevery=8 (cleaner)')\\n\\nfor ax in [ax1, ax2]:\\n    ax.set_xlabel('X')\\n    ax.set_ylabel('Y')\\n    ax.grid(True, alpha=0.3)\\n\\nplt.show()\\n
\\n\\n
\\n\\n

Frequently Asked Questions

\\n\\n

Different shapes of yerr?

\\n
    \\n
  • Scalar: Same error for all points.
  • \\n
  • 1D array (N,): Symmetric error for each point (same upper/lower).
  • \\n
  • 2D array (2, N): First row is lower error, second row is upper error.
  • \\n
\\n\\n

How to remove the line connecting data points?

\\n

Set fmt to a marker-only format, such as 'o', 's', '^', omitting the line style part.

\\n\\n

Image 2: Matplotlib Reference Documentation Matplotlib Reference Documentation

← Matplotlib Ref FigureMatplotlib Ref Colorbar β†’