Plotting errors bars with “plot” without using axis environment from pgfplots

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) -- coordinate (x axis mid) (4,0) node[anchor=west] {$x$};
    draw (0,-1) -- coordinate (y axis mid) (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?

Leave a Reply

Your email address will not be published. Required fields are marked *