Tx · LaTeX & TikZ

How to Add Error Bars to a Bar Plot in PGFPlots

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose error values

    Decide the vertical uncertainty for each bar, such as a standard deviation or confidence-interval half-width.

  2. 2. Enable explicit errors

    Add y dir=both and y explicit in the plot’s error bars options.

  3. 3. Pair values and errors

    Give each coordinate its bar height and its error magnitude.

  4. 4. Compile and check

    Inspect the caps and axis limits to confirm the bars show the intended intervals.

To add vertical error bars to a bar chart in PGFPlots, enable explicit y errors and supply an error magnitude for each plotted value. The example below uses named categories and symmetric error bars; you can replace the sample values and uncertainties with your own data.

Minimal working example

This complete document draws three bars. Each bar has an error bar extending the stated amount above and below its height.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar,
    bar width=18pt,
    symbolic x coords={A,B,C},
    xtick=data,
    ymin=0,
    ylabel={Measurement},
    enlargelimits=0.2,
]
\addplot+[
    error bars/.cd,
    y dir=both,
    y explicit,
] coordinates {
    (A,2.0) +- (0,0.25)
    (B,3.1) +- (0,0.40)
    (C,2.6) +- (0,0.20)
};
\end{axis}
\end{tikzpicture}
\end{document}

The example needs the pgfplots package; the standalone class is used to produce a tightly cropped figure. Compile it with a LaTeX engine such as pdfLaTeX. compat=1.18 selects a defined PGFPlots compatibility level; if your installed version is older, use a compatibility level supported by that installation.

How the error-bar syntax works

The relevant settings are:

  • error bars/.cd switches to PGFPlots’ error-bar options.
  • y dir=both tells PGFPlots to draw the error in both vertical directions. Use y dir=plus or y dir=minus if your errors should extend only upward or downward.
  • y explicit says that the error sizes are supplied in the coordinates, rather than calculated from a separate setting.
  • (A,2.0) +- (0,0.25) gives the x position, plotted y value, and symmetric x and y error sizes. Here the x error is zero, and the y error is 0.25.

For a bar with value yy and symmetric error ee, the displayed interval is [y−e,y+e][y-e,y+e]. Thus the first bar has endpoints 1.75 and 2.25. The error values are distances from the plotted value, not the lower and upper endpoints themselves.

The example uses symbolic x coords to label the bars A, B, and C. For numerical x positions, remove symbolic x coords and use coordinates such as (1,2.0) +- (0,0.25) instead. The error-bar options are the same in either case.

Choose what the error bars mean

PGFPlots draws the values you provide; it does not decide whether they represent standard deviations, standard errors, confidence intervals, or another measure. For example, if a bar is a sample mean of 12 and the confidence interval runs from 10.5 to 13.5, use a plotted value of 12 and an error size of 1.5. If upper and lower uncertainties differ, calculate and provide those two distances separately using the error-bar syntax supported by your PGFPlots version, or use its table-based error input. Do not enter 10.5 and 13.5 as error magnitudes: those are endpoints, not distances from 12.

Check that the vertical axis includes the full error-bar range. In the example, enlargelimits=0.2 adds space around the plotted data. If you set an explicit ymax, make sure it is not below the top of the largest error bar, or the error bar may be clipped.

Common mistakes

  • Leaving out y explicit: PGFPlots needs to know that the coordinate data include explicit error values.
  • Using the endpoint as the error size: For a bar at 5 with limits 4 and 7, the lower and upper error distances are 1 and 2, not 4 and 7.
  • Forgetting the direction: Set y dir=both for errors above and below the bar. A one-sided direction will not draw the other half.
  • Confusing error-bar choices: The plot command only draws the supplied sizes. Decide separately whether your chart should show a standard deviation, a standard error, a confidence interval, or something else.
  • Clipping the caps: Review ymin, ymax, and the axis limits if an error bar reaches the plot boundary.

For more control over styling or data loaded from a table, consult the PGFPlots error-bars documentation. The core idea remains the same: specify the direction, indicate that errors are explicit, and provide the appropriate error distance for each bar.

References

  1. PGFPlots Manual: Error Bars

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.