Matplotlib errorbar() Function |
\\n\\n Matplotlib Reference Documentation
The errorbar() function is used to plot line charts with error bars, displaying the uncertainty range of measured values above each data point.
Widely used in scientific experiments, statistical analysis, and engineering data visualization.
\\n\\nFunction Definition
\\n\\npyplot Interface
\\nmatplotlib.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
\\nAxes.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
\\n\\n| Parameter | \\nType | \\nDescription | \\n
|---|---|---|
| x, y | \\narray-like | \\nCoordinates of data points | \\n
| yerr / xerr | \\nfloat or array-like | \\nError 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. | \\n
| fmt | \\nstr | \\nFormat string for data points, e.g., 'o' (circle), 's' (square), 'o-' (circle + line) | \\n
| ecolor | \\ncolor | \\nColor of the error bar lines | \\n
| elinewidth | \\nfloat | \\nWidth of the error bar lines | \\n
| capsize | \\nfloat | \\nLength of the horizontal caps at error bar ends (in points) | \\n
| capthick | \\nfloat | \\nThickness of the horizontal caps at error bar ends | \\n
| barsabove | \\nbool | \\nIf True, error bars are drawn above the data points | \\n
| lolims / uplims | \\narray-like of bool | \\nMark lower/upper bounds in y-direction (only single-sided arrows) | \\n
| xlolims / xuplims | \\narray-like of bool | \\nMark lower/upper bounds in x-direction | \\n
| errorevery | \\nint | \\nDraw error bars every N data points (to reduce visual clutter for dense data) | \\n
\\n\\n\\n\\n
errorbar()returns anErrorbarContainerobject containing(plotline, caplines, barlinecols), allowing separate access to the main line and error bar lines.
\\n\\n
Usage Examples
\\n\\nExample 1: Basic Error Bars
\\n\\nExamples
\\n\\nimport 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\\nExample 2: Asymmetric Errors (Different Upper/Lower)
\\n\\nExamples
\\n\\nimport 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\\nExample 3: Bidirectional Error Bars (Both X and Y Directions)
\\n\\nExamples
\\n\\nimport 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\\nExample 4: Using errorevery to Reduce Visual Clutter
\\n\\nExamples
\\n\\nimport 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\\nDifferent 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
How to remove the line connecting data points?
\\nSet fmt to a marker-only format, such as 'o', 's', '^', omitting the line style part.
YouTip