3

I'm new to pgfplots, but it seems like a really useful package. However, I can't quite figure out how to use error bars.

Below is the code to my column chart, where some columns should include an error bar. I figured out that I needed to use a different \addplot command for the error bars, but they are still not showing.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width  = 0.85*\textwidth,
    height = 8cm,
    major x tick style = transparent,
    ybar=2*\pgflinewidth,
    bar width=14pt,
    ymajorgrids = true,
    ylabel = {Concentration [mg/L]},
    symbolic x coords={1,2,3,4},
    xtick = data,
    scaled y ticks = false,
    enlarge x limits=0.25,
    ymin=0,
    legend cell align=left,
    legend style={
            at={(1,-0.40)},
            anchor=south east,
            column sep=1ex
    }
]
    \addplot+[error bars/.cd,,addplot[y dir=both,y explicit]
        coordinates {
        (1, 133.1) 
        (2,251.7) 
        (3,372.4) 
        (4,517.9)
        };

    \addplot+[error bars/.cd,,addplot[y dir=both,y explicit]
         coordinates {
         (1,97.4) +- (0.0, 11.0)
         (2,175.5) +- (0.0, 3.86)
         (3,290.8) +- (0.0, 3.14)
         (4,482.8) +- (0.0, 15.3)
         };

    \addplot+[error bars/.cd,,addplot[y dir=both,y explicit]
         coordinates {
         (1,116.9)
         (2,150.2) +- (0.0, 0.56)
         (3,289.0) +- (0.0, 13.2)
         (4,429.6) +- (0.0, 29.0)
         };

    \legend{Untreated solution,Treated with A,Treated with B}
\end{axis}
\end{tikzpicture}
\end{document}

Do anyone know why this doesn't work? Is it something in my \begin{axis} settings that prevents them from showing?

Thanks in advance!

3
  • Also, if anyone has a better idea on how to achieve this, feel free to let me know. I don't necessarily have to use pgfplots, but I just want a grouped column chart with error bars. Jan 8, 2019 at 18:07
  • Welcome to TeX.SE! I've never seen something like \addplot+[error bars/.cd,,addplot[y dir=both,y explicit]. Are you sure you can nest the addplots lie this?
    – user121799
    Jan 8, 2019 at 18:09
  • Thanks, I must have inserted them wrongly. It works now! Jan 8, 2019 at 18:15

1 Answer 1

2

If I do not nest addplots, I get

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width  = 0.85*\textwidth,
    height = 8cm,
    major x tick style = transparent,
    ybar=2*\pgflinewidth,
    bar width=14pt,
    ymajorgrids = true,
    ylabel = {Concentration [mg/L]},
    symbolic x coords={1,2,3,4},
    xtick = data,
    scaled y ticks = false,
    enlarge x limits=0.25,
    ymin=0,
    legend cell align=left,
    legend style={
            at={(1,-0.40)},
            anchor=south east,
            column sep=1ex
    }
]
    \addplot+[error bars/.cd,y dir=both,y explicit]
        coordinates {
        (1, 133.1) 
        (2,251.7) 
        (3,372.4) 
        (4,517.9)
        };

    \addplot+[error bars/.cd,y dir=both,y explicit]
         coordinates {
         (1,97.4) +- (0.0, 11.0)
         (2,175.5) +- (0.0, 3.86)
         (3,290.8) +- (0.0, 3.14)
         (4,482.8) +- (0.0, 15.3)
         };

    \addplot+[error bars/.cd,y dir=both,y explicit]
         coordinates {
         (1,116.9)
         (2,150.2) +- (0.0, 0.56)
         (3,289.0) +- (0.0, 13.2)
         (4,429.6) +- (0.0, 29.0)
         };

    \legend{Untreated solution,Treated with A,Treated with B}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

i.e. error bars appear wherever you have +- in your data.

3
  • Yes! Thanks a lot, this works like a charm. Don't know why I wrote it that way, I guess I wasn't paying enough attention when changing the addplot to addplot+. One further question, though. Is it possible to change the colours of the bars? Jan 8, 2019 at 18:17
  • @ChristianP. Certainly, see for example tex.stackexchange.com/a/468836 Jan 8, 2019 at 18:19
  • @ChristianP. Yes. Please see this recent question, which has two answers, one in which the colors are set directly, and another one in which the cycle list gets modified.
    – user121799
    Jan 8, 2019 at 18:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .