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.

For plots with a lot of points it often looks very crowded if each point has its own error bar. Is there an easy way to only draw certain error bars in pgfplots? I could not find any option besides drawing the data twice. So in short: is there a command such as error mark indices or a similar option to influence the drawing of error marks?

Here is my MWE, this is basically how it should look like:

\documentclass[tikz,preview]{standalone}

\usepackage{tikz,pgfplots,filecontents}
\pgfplotsset{compat=1.7} 

\begin{filecontents*}{data.dat}
X Y Y_error
1 1.39 0.1
2 2.44 0.1
3 3.62 0.1
4 4.81 0.1
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    clip mode=individual,
    ]
    \addplot+[each nth point=4,forget plot,error bars/.cd,y dir=both,y explicit] 
                table[x=X,y=Y,y error=Y_error] {data.dat};
    \addplot+[] table[x=X,y=Y,y error=Y_error] {data.dat};
    % these command do not seem to exist:
    % error mark repeat=4
    % error mark indices={1}
\end{axis}
\end{tikzpicture} 

\end{document}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

That key doesn't exist yet, but you can create your own relatively easily. You can hook into the draw error bar/.code key to introduce a check whether the current error bar number is divisible by some number, and if it isn't, the opacity of the error bar is set to 0. To check the error bar number, you'll have to introduce a new counter that is stepped after every bar.

Then you can say

\addplot+[
        error bars/.cd,
            y dir=both,
            y explicit,
            every nth mark=3
    ] table[x=X,y=Y,y error=Y_error] {data.dat};

to get

or every nth mark=2

to get


This is the relevant code chunk:

\newcounter{marknumber}
\pgfplotsset{
    error bars/every nth mark/.style={
        /pgfplots/error bars/draw error bar/.prefix code={
            \pgfmathtruncatemacro\marknumbercheck{mod(floor(\themarknumber/2),#1)}
            \ifnum\marknumbercheck=0
            \else
                \begin{scope}[opacity=0]
            \fi
        },
        /pgfplots/error bars/draw error bar/.append code={
            \ifnum\marknumbercheck=0
            \else
                \end{scope}
            \fi
            \stepcounter{marknumber}    
        }
    }
}

And this is a full example document:

\documentclass[tikz,preview, border=5mm]{standalone}

\usepackage{tikz,pgfplots,filecontents}
\pgfplotsset{compat=1.7} 

%%% Code for "every nth mark" starts here...
\newcounter{marknumber}
\pgfplotsset{
    error bars/every nth mark/.style={
        /pgfplots/error bars/draw error bar/.prefix code={
            \pgfmathtruncatemacro\marknumbercheck{mod(floor(\themarknumber/2),#1)}
            \ifnum\marknumbercheck=0
            \else
                \begin{scope}[opacity=0]
            \fi
        },
        /pgfplots/error bars/draw error bar/.append code={
            \ifnum\marknumbercheck=0
            \else
                \end{scope}
            \fi
            \stepcounter{marknumber}    
        }
    }
}
%%% ... and ends here

\begin{filecontents*}{data.dat}
X Y Y_error
1 1.39 0.5
2 2.44 0.5
3 3.62 0.5
4 4.81 0.5
5 1.39 0.5
6 2.44 0.5
7 3.62 0.5
8 4.81 0.5
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    clip mode=individual,
    ]
    \addplot+[
            error bars/.cd,
                y dir=both,
                y explicit,
                every nth mark=2
        ] table[x=X,y=Y,y error=Y_error] {data.dat};
\end{axis}
\end{tikzpicture} 

\end{document}
share|improve this answer
    
Oh that is just great. Thank you Jake! –  Alexander Feb 22 '13 at 11:01
1  
sorry for this comment but: Jake I Love You so much! ;-) - it seems you already have written answers for all my questions :-) –  Thomas Apr 14 at 18:58
add comment

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.