2

In the axis environment of the pgfplots package there is a simple way to add error bars:

\documentclass{minimal}
\usepackage{filecontents,pgfplots,tikz}
\usetikzlibrary{plotmarks}

\begin{filecontents}{plot2.data}
#x  y   yError
0   0.5 0.3
1   0.5 0.1
2   0.5 -0.3
3   0.5 -0.6
\end{filecontents}

\begin{document}
\begin{tikzpicture}[x=1cm,y=1cm]
    \begin{axis}
    \addplot+[error bars/.cd,y dir=plus,y explicit,error bar style={color=red}]
        table [x index=0,y index=1,y error index=2] {plot2.data};
    \end{axis}
\end{tikzpicture}
\end{document}

Error bars with pgfplots

In some situations however (e.g. when you want to draw some points or lines on top of an existing image), the tikz-native plot function is more convenient than axis:

\documentclass{minimal}
\usepackage{filecontents,tikz}

\begin{filecontents}{plot1.data}
#x  y
0   0.5
1   0.5
2   0.5
3   0.5
\end{filecontents}

\begin{document}
\begin{tikzpicture}[x=1cm,y=1cm]
    \draw[->] (-1,0) -- (4,0) node[anchor=west] {$x$};
    \draw (0,-1) -- (0,1) node[anchor=south] {$f(x)$};
    \draw plot[mark=*, mark options={fill=white}] file {plot1.data};
\end{tikzpicture}
\end{document}

How to add error bars to this?

Is there a way to add similar error bars within plot?

  • I don't know if there is an alternative, at least datavisualization does not seem to provide error bars, but I may be wrong. However, I also do not understand what precisely is meant by "In some situations however (e.g. when you want to draw some points or lines on top of an existing image), the tikz-native plot function is more convenient than axis". How's that? I don't see a problem in putting an axis on top of some other stuff. Notice that the axis environment accepts keys like shift and you can turn off or control the axis scaling. – marmot Nov 10 '18 at 20:09
  • @marmot In plot each data point is drawn at those coordinates: the first line in plot1.data is a point at exactly (0,0.5). In axis this is not straightforward: (0,0) is the bottom-left corner of the plot, and the spacing between the datapoints is not simply the spacing between the data. So if you only want to put a list of points at specific coordinates (as when plotting on top of an existing image), plot is easy and straightforward. Of course it could be done with axis by removing the axes, shifting it, and changing the scale, but it would be a bit hacky and inconvenient – Betohaku Nov 12 '18 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.