Error Bars & Confidence Bands

An error bar is a small line drawn through a data point that shows how much that value could vary, turning a single number into an honest range of uncertainty around your measurement.

Learn Error Bars & Confidence Bands in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll add symmetric and asymmetric error bars with ax.errorbar(), put error bars on a bar chart, and draw a smooth shaded confidence band.

ax.errorbar() works like ax.plot() but adds an uncertainty range to each point. Pass yerr to draw vertical error bars and xerr for horizontal ones. The fmt argument sets the marker and line style — use "o" for points only or "-o" to connect them. Add capsize to put readable caps on the ends.

What you'll see: five teal points connected by a line that climbs from left to right. A short vertical bar runs through each point with little caps at its ends — taller bars (like the fourth point) signal more uncertainty in that reading.

Sometimes a value could be more wrong in one direction than the other. For asymmetric error bars, pass yerr as a two-row array: the first row is the distance below each point and the second is the distance above . You can also attach error bars directly to a bar chart by passing yerr to ax.bar() .

What you'll see: three blue bars showing model accuracy. A thin black error bar rises from the top of each one, and because the errors are asymmetric, the cap above the bar is closer than the cap below it — the downside risk is larger than the upside.

When you have a smooth curve with uncertainty at every point, dozens of error bars become noise. Instead, draw the line with ax.plot() and wrap it in a translucent ribbon using ax.fill_between(x, y - err, y + err, alpha=0.3) . The band shows the same uncertainty as a continuous shaded region — far cleaner for dense data.

What you'll see: a wavy orange line trending upward, hugged by a soft orange ribbon. The ribbon starts narrow on the left and widens toward the right, visually telling the reader that the forecast grows less certain further out.

Replace each ___ to plot points with vertical error bars and end caps.

For asymmetric errors yerr must have shape (2, N) — a lower row and an upper row, each the same length as your data.

The default capsize is 0. Set capsize=4 or higher to make the end caps visible.

Negative lower bounds break log scales. Clip the lower error so y - err stays positive before plotting.

Show the same uncertain data two ways: discrete error bars on the left, and a smooth shaded band on the right.

Lesson complete — you can show uncertainty honestly!

You drew symmetric and asymmetric error bars with ax.errorbar(), attached error bars to a bar chart, tuned capsize and fmt, and wrapped a smooth line in a shaded confidence band.

🚀 Up next: Filling Areas (fill_between) — shade the region between two curves.

Practice quiz

Which Axes method draws data points with vertical uncertainty bars?

  • ax.plot()
  • ax.errorbar()
  • ax.scatter()
  • ax.bar()

Answer: ax.errorbar(). ax.errorbar() works like ax.plot() but adds an uncertainty range to each point.

Which argument draws vertical error bars?

  • xerr
  • yerr
  • verr
  • barerr

Answer: yerr. yerr draws vertical error bars; xerr draws horizontal ones.

What does the capsize argument control?

  • The width of the end caps in points
  • The number of points
  • The line color
  • The marker size

Answer: The width of the end caps in points. capsize sets the width, in points, of the little horizontal caps at the ends of each error bar.

What is the default value of capsize?

  • 4
  • 5
  • 0
  • 1

Answer: 0. The default capsize is 0, so no caps appear until you set a positive value.

For asymmetric errors, what shape must yerr have for N points?

  • (N,)
  • (N, 2)
  • (2, N)
  • (1, N)

Answer: (2, N). Asymmetric yerr is a (2, N) array: the first row is the lower error, the second the upper.

In a two-row yerr, what does the FIRST row represent?

  • The lower (below) error
  • The upper (above) error
  • The x error
  • The mean value

Answer: The lower (below) error. The first row is always the lower error and the second row is the upper error.

What does the fmt argument in ax.errorbar() set?

  • The figure size
  • The marker and line style
  • The axis limits
  • The color map

Answer: The marker and line style. fmt sets the marker and line style, just like the format string in ax.plot().

How do you draw a smooth shaded confidence band around a line?

  • ax.errorbar()
  • ax.bar(yerr=...)
  • ax.fill_between(x, y-err, y+err)
  • ax.hist()

Answer: ax.fill_between(x, y-err, y+err). ax.fill_between(x, y - err, y + err) shades a continuous ribbon of uncertainty.

Can you attach error bars directly to a bar chart?

  • No, never
  • Yes, by passing yerr to ax.bar()
  • Only with ax.scatter()
  • Only with fill_between()

Answer: Yes, by passing yerr to ax.bar(). Passing yerr to ax.bar() draws an error bar rising from each bar.

When is a shaded band preferable to discrete error bars?

  • For a handful of discrete points
  • For a smooth curve with uncertainty at every x
  • Never
  • Only for bar charts

Answer: For a smooth curve with uncertainty at every x. A band reads better for a smooth curve with uncertainty at every x; bars suit a few discrete points.