Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

A data file contains four columns: x, y, error on y, class. class is an integer which I feed to scatter src so i get a graph that has different colors for different classes. However, the error bars don't have class colors.

\begin{tikzpicture}
\begin{axis}[colorbar right, colorbar sampled, colorbar style={samples=3},
             point meta min=-0.5, point meta max=1.5,
             scatter/use mapped color={draw=mapped color, fill=mapped color}]
\addplot[scatter, scatter src=\thisrow{class}, 
      error bars/.cd, y dir=both, y explicit, error bar style={color=mapped color}] 
      table[x=x,y=y,y error=err] {
x y err class
0 0 1   0
1 1 1   0
2 0 1   1
3 2 2   1
};
\end{axis}
\end{tikzpicture}

In that case, the error bars are black. If instead of error bar style={color=mapped color} I use error bar style={color=red},the error bars are red.

example

How can I get the right colors for the error bars, e.g. those of the points?

share|improve this question
    
This is going to be quite difficult, because at the time when the error bars are drawn, the point meta data (which determines the color in the scatter plot) is not readily available. You might want to post a feature request at the pgfplots sourceforge site –  Jake Nov 6 '11 at 23:10
    
that's bad news... –  yannick Nov 7 '11 at 12:27
    
What about drawing you graphs with GNUPlot? There's a great reference site here. I'm sure you can find a way to customize your plot to your liking with it :) –  mpr Nov 13 '11 at 6:46

1 Answer 1

Until this is implemented properly in PGFplots, you can abuse the scatter/@pre marker code key for drawing colored error bars. I've defined a key error bars with mapped color=<error source>, which will set the necessary keys, so that

\addplot [
    scatter,
    scatter src=\thisrow{class},
    error bars with mapped color=err,
    error bars/.cd,
        y dir=both,
        y explicit
] 
      table[x=x,y=y,y error=err] {
x y err class
0 0 1   0
1 1 1   0
2 0 1   1
3 2 2   1
};
\end{axis}

will give you

A couple of problems with this solution:

The colored error bars at the moment assume that the error is absolute, and in positive and negative y-direction. Different error bar types would have to be adjusted manually in the error bars with mapped color style (it wouldn't be enough to change the relevant error bars styles).

When the plot marks and the colored error bars are drawn, the coordinate system is scaled in a bit of an unusual way (in increments of powers of ten) depending on the data range. I haven't figured out how to access the scaling factor automatically, so you might have to set this factor manually depending on your data range.

Here's the code:

\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}[colorbar right, colorbar sampled, colorbar style={samples=3},
             point meta min=-0.5, point meta max=1.5,
             scatter/use mapped color={draw=mapped color, fill=mapped color}]
\addplot [
    scatter,
    scatter src=\thisrow{class},
    error bars with mapped color=err,
    error bars/.cd,
        y dir=both,
        y explicit
] 
      table[x=x,y=y,y error=err] {
x y err class
0 0 1   0
1 1 1   0
2 0 1   1
3 2 2   1
};
\end{axis}
\end{tikzpicture}


\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.