[Tex/LaTex] Define error bar color in scatter class

pgfplotspgfplotstabletikz-pgf

I'm using scatter/classes to define color and markers for a plot, and I would like to specify the error bar color this way as well (to match the marker color). I have seen the error bar colors in scatter plot post, but I don't know how to use the scatter class color, rather than the mapped color. Specifying the error bar color in the scatter/classes does not work.

\documentclass[article]{standalone}               
\usepackage{graphicx}
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.7}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
      \addplot [scatter,
        only marks,
        scatter src=explicit symbolic,
        scatter/classes={
          A0={mark=o,red,/pgfplots/error bars/.cd,
            error mark options={draw=red}},
          A1={mark=square,blue}},
        error bars/.cd,
        y dir=both,
        y explicit] 
      table[x=x,y=y,y error=err,meta=class,row sep=crcr] {
        x y err class\\
        0 0 1   A0\\
        1 1 1   A0\\
        2 0 1   A1\\
        3 2 2   A1\\
      };
    \end{axis}
\end{tikzpicture}
\end{document}

Error bars do not match scatter class

Best Answer

One way to do this is to use the scatter/@pre marker code to "manually" draw the error bars and markers. This will automatically use the drawing style of the plot markers (including the color). A drawback is that PGFPlots doesn't adjust the axis limits automatically to accommodate the error bars. To work around this, you can also draw "conventional" but transparent error bars.

Here's a style error bars with color that takes the name of the column containing the error values as a mandatory argument, and implements the above approach. The style must be used after your scatter/classes definition, as it would otherwise be overridden.

error bars with color/.style={
    visualization depends on=\thisrow{#1} \as \error,
    visualization depends on=y \as \y,
    scatter/@pre marker code/.append code={ % Homebrew color bars
            \draw (0,0) -- +(axis direction cs:0,-\error) -- +(axis direction cs:0,\error);
            \draw (0,0) plot [mark=-] coordinates {(axis direction cs:0,\error) (axis direction cs:0,-\error)};
    },
    error bars/.cd, % Invisible color bars, to get the right axis limits
            y dir=both,
            y explicit,
            error bar style={opacity=0},
    /pgfplots/.cd
}

Full code:

\documentclass[border=5mm]{standalone}     
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.7}


\pgfplotsset{
    error bars with color/.style={
        visualization depends on=\thisrow{#1} \as \error,
        visualization depends on=y \as \y,
        scatter/@pre marker code/.append code={ % Homebrew color bars
                \draw (0,0) -- +(axis direction cs:0,-\error) -- +(axis direction cs:0,\error);
                \draw (0,0) plot [mark=-] coordinates {(axis direction cs:0,\error) (axis direction cs:0,-\error)};
        },
        error bars/.cd, % Invisible color bars, to get the right axis limits
                y dir=both,
                y explicit,
                error bar style={opacity=0},
        /pgfplots/.cd
    }
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
      \addplot [scatter,
        only marks,
        scatter src=explicit symbolic,
        scatter/classes={
          A0={mark=o,red,
          },
          A1={mark=square,blue}
        },
        error bars with color=err
         ] 
      table[x=x,y=y,y error=err,meta=class,row sep=crcr] {
        x y err class\\
        0 0 1   A0\\
        1 1 1   A0\\
        2 0 1   A1\\
        3 2 2   A1\\
      };
    \end{axis}
\end{tikzpicture}
\end{document}
Related Question