11

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}

1 Answer 1

14

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}
1
  • 2
    sorry for this comment but: Jake I Love You so much! ;-) - it seems you already have written answers for all my questions :-)
    – Thomas
    Commented Apr 14, 2014 at 18:58

You must log in to answer this question.

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