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}
