2

The following MWE shows two bar plots with error bars.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\pgfplotstableread{
x y1 y1_err y2 y2_err
0 1  .1     2  .2
1 2  .2     3  .3
2 3  .3     4  .4
}\loadedtable

\begin{tikzpicture}
  \begin{axis}[ybar,bar width=14pt ]
    \addplot[
      style={red, fill=red},
      error bars/.cd,
      error bar style={
        thick,
      },
      y dir=both,
      y explicit,
    ] table[
      x=x,
      y=y1,
      y error=y1_err,
    ] {\loadedtable};
    \addplot[
      style={blue, fill=blue},
      error bars/.cd,
      error bar style={
        thick,
      },
      y dir=both,
      y explicit,
      ]
      table[x=x, y=y2, y error=y2_err] {\loadedtable};
  \end{axis}
\end{tikzpicture}

\end{document}

Error bars with plots

One of the downsides is that I have to repeat a lot of the same error-bar style code, whereas I would ideally like to be able to do something like:

\pgfplotsset{errorBars/.style={
    error bars/.cd,
    error bar style={
      thick,
    },
    y dir=both,
    y explicit,
  }
}

Unfortunately, \addplot[errorBars/.style,style={red, fill=red}] does not actually show the error bars as desired.

How can I set the style for all error bars in one particular tikzpicture?

1 Answer 1

2

With

\pgfplotsset{
  errorBars/.style={
    error bars/error bar style={
      thick,
    },
    error bars/y dir=both,
    error bars/y explicit,
  }
}

you can use \addplot[errorBars,style={red, fill=red}] or \addplot[style={red, fill=red},errorBars]. If all plots inside an axis environment should have error bars with this style, you can use every axis plot/.append style={errorBars} as an option for the axis environment.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=1.13}

\pgfplotsset{
  errorBars/.style={
    error bars/error bar style={
      thick,
    },
    error bars/y dir=both,
    error bars/y explicit,
  }
}

\begin{document}

\pgfplotstableread{
x y1 y1_err y2 y2_err
0 1  .1     2  .2
1 2  .2     3  .3
2 3  .3     4  .4
}\loadedtable

\begin{tikzpicture}
  \begin{axis}[
    ybar,
    bar width=14pt,
    every axis plot/.append style={errorBars}
    ]
    \addplot[
      %errorBars,
      style={red, fill=red}
    ] table[
      x=x,
      y=y1,
      y error=y1_err,
    ] {\loadedtable};
    \addplot[
      style={blue, fill=blue},
      %errorBars
      ]
      table[x=x, y=y2, y error=y2_err] {\loadedtable};
  \end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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