1

I want a grouped scatter plot with xy error bars and a legend. So each point is a different group, with its own color, mark, and legend entry. The error bars should have the same color as the mark they belong to.

Here is an edited example from this answer:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
    error bars with mapped color/.style={
        disabledatascaling,
        visualization depends on=\thisrow{#1} \as \error,
        visualization depends on=\thisrow{y} \as \y,
        scatter/@pre marker code/.append style={
            /pgfplots/error bars/.cd,
            error mark options={draw=mapped color},
            error mark=|,
            draw error bar={(0,0)}{(0,\error*100)}, % *100 to correct for the scaling
            draw error bar={(0,0)}{(0,-\error*100)} % might have to be adjusted (0.1,1,10,100,...)
        },
        scatter/@post marker code/.append code={}
    }
}

\begin{tikzpicture}
\begin{axis}[
             scatter/use mapped color={draw=mapped color, fill=mapped color},
         scatter/classes={ a={mark=*,draw=red,fill=red!50},b={mark=square*,draw=green,fill=green!50},c={mark=triangle*,draw=magenta,fill=magenta!50} }
         ]
\addplot [
    scatter,
    only marks,
    scatter src=\thisrow{class},
    scatter src=explicit symbolic,
    error bars with mapped color=err,
    error bars/.cd,
        y dir=both,
        y explicit
] 
      table[x=x,y=y,y error=err,meta=meta] {
x y err class meta
0 0 1   0  a
1 1 .1   0  a
2 0 1   1  b
3 -.5 .2   2  c
};
\legend{Worlds,People,Barnacles}
\end{axis}
\end{tikzpicture}
\end{document}

But you'll notice that the "brackets" of the error bars are not colored correctly. I haven't managed creating x-error bars at the same time.

Maybe, by now, there is an altogether better solution than @Jake suggested?

enter image description here

12.yakir
  • 419

1 Answers1

1

I figured the following, while very ugly, works relatively well:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{myerr/.append style={only marks,error bars/.cd, y dir=both,y explicit, x dir=both,x explicit} }
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[myerr] coordinates { (0,0) +- (0.5,0.1)};
\addlegendentryexpanded{Worlds}
\addplot+[myerr] coordinates { (-1,1) +- (0.7,1.1)};
\addlegendentryexpanded{People}
\addplot+[myerr] coordinates { (-2,4) +- (2.7,2.1)};
\addlegendentryexpanded{Barnacles}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

I just added an addplot with error details per point. Colors, legend, etc are taken care of by pgfplots, and I can automate this type of code from the same program I use to generate the data points (in my case, Julia).

12.yakir
  • 419