1

I have a number of data series to plot with error bars, but since they might overlap in the middle, I'd like to show error bars for only the first and last points. All the data points will have the same error bars, so showing the first and last should be sufficient to show the range.

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{
    compat=1.12,
    every axis plot/.append style={
        error bars/y dir=both,
        error bars/x dir=both,
        error bars/x fixed=1,
        error bars/y fixed=1,
    },
}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot coordinates {(1,1) (3,3) (5,5)};
            \addplot coordinates {(1,5) (3,3) (5,1)};
        \end{axis}
    \end{tikzpicture}
\end{document}

1 Answer 1

2

We can define a new style that checks the coordinate index and plots the error bars for the appropriate coordinates. This works for the coordinates and table plot types, as far as I know. This code is based on Jake's answer to Specific error bars in pgfplots possible? Note that the \coordindex is zero-based, but the \numcoords is one-based, so we have to subtract 1 from \numcoords to ensure we get the last point.

error bars/first last error bars/.style={
    /pgfplots/error bars/draw error bar/.prefix code={
        \pgfmathtruncatemacro\marknumbercheck{%
            or((\coordindex==0), (\coordindex==(\numcoords-1)))%*See the note
        }
        \ifnum\marknumbercheck=0
            \begin{scope}[opacity=0]
        \fi
    },
    /pgfplots/error bars/draw error bar/.append code={
        \ifnum\marknumbercheck=0
            \end{scope}
        \fi
    }
}

Note: The last of these parentheses is actually unnecessary, as shown in the previously linked Q/A. Is this a bug, or a feature?


\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{
    compat=1.12,
    every axis plot/.append style={
        error bars/y dir=both,
        error bars/y fixed=1,
        error bars/first last error bars,
    },
    error bars/first last error bars/.style={
        /pgfplots/error bars/draw error bar/.prefix code={
            \pgfmathtruncatemacro\marknumbercheck{or((\coordindex==0), (\coordindex==(\numcoords-1)))}
            \ifnum\marknumbercheck=0
                \begin{scope}[opacity=0]
            \fi
        },
        /pgfplots/error bars/draw error bar/.append code={
            \ifnum\marknumbercheck=0
                \end{scope}
            \fi
        }
    },
}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot coordinates {(1,1) (3,3) (5,5)};
            \addplot coordinates {(1,5) (3,3) (5,1)};
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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