Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I read data from a table to create a plot in Pgfplots:

x    y   sigma
1    2    0.3
2    4.1  0.4

where sigma is the standard error associated with y.

Since y and sigma together are are standard normal estimators, the range [y-sigma,y+sigma] can be interpreted as a 68.3% confidence interval. ([1.7,2.3] in the example.) This is the range for which the standard "error bar" is shown if you add errors to the data points in PGFplots. Now, I would like to instead show the range [y-1.96*sigma,y+1.96*sigma], corresponding to the 95% confidence interval. One easy way would be to add a row to the table:

x    y   sigma  r95
1    2    0.3   0.588 %=1.96*0.3
2    4.1  0.4   0.784 %=1.96*0.4

and then use r95 as variable for the errors. But in my case, that would be a lot of work. Is there a way to accomplish the same from within PGFPlots, without changing the tables? My current PGF plots is something like:

\begin{axis}[
height=6cm,
width=8cm,
grid=major,
title=Figure title,
xlabel=The $x$ axis,
ylabel=The $y$ axis,
legend pos=north west,
]
\addplot+[only marks,mark=asterisk,error bars/.cd,y dir=both,y explicit]   
table [x=x,y=y,y error = sigma] {data.txt};
\legend{$S$}
\end{axis}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

You can use a mathematical expression for the error bars if you say y error expr=\thisrow{sigma}*1.96 instead of y error=sigma:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
height=6cm,
width=8cm,
grid=major,
title=Figure title,
xlabel=The $x$ axis,
ylabel=The $y$ axis,
legend pos=north west,
]
\addplot+[only marks,mark=asterisk,error bars/.cd,y dir=both,y explicit]   
table [x=x,y=y,y error expr= \thisrow{sigma}*1.96] {
x    y   sigma
1    2    0.3
2    4.1  0.4
};
\legend{$S$}
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
Thanks, that saves an awful lot of work. I tried something like this, but didn't find the "Mathematical Expressions And File Data" section in PGFPlots; very useful. –  willem Apr 25 '13 at 9:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.