# Plotting error Bars in PGF plots

I want to plot error bars in PGFplots using the following Latex code but its not showing any error neither the error bars.

I know I should mention the error values explicitly but what if i do not want to calculate errors (standard deviation) of every point manually.

Any help regarding this would be highly appreciated. Thanks.

\documentclass[conference]{IEEEtran}
\ifCLASSINFOpdf
\else
\fi
\hyphenation{op-tical net-works semi-conduc-tor}
\usepackage[pdftex]{graphics}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=1.7}

\pgfplotsset{footnotesize,samples=10}
\begin{figure}[t]
\centering
\begin{tikzpicture}
\begin{axis}[ % The height and width argument only apply to the actual axis
title style={at={(0.3,0.2)},anchor=north,yshift=-0.1},
title =\textbf{Correlation= 0.672658953},
height=5cm,
width=8cm,
legend style={at={(0.5,1.3)},anchor=north},
ylabel={PDR},
xlabel={LQI},
xmin=85, xmax=120,
ymin=80, ymax=100,
xtick= {85,90,95,100,105,110,115,120},
ytick= {80,85,90,95,100},
legend columns=4,
ymajorgrids=true,
grid style=dashed,
]
mark=o,
mark size=1.4pt,
only marks,
]
plot [error bars/.cd, y dir = both, y explicit]
coordinates {
(104,   97)(98  ,97)(105,   98)(103,    97)(104,    98)(85  ,80)(91 ,86)(103,   97)(105,    98)
};
%\label{plot_two}

mark size=1.4pt,
only marks,
mark=o,
]
plot [error bars/.cd, y dir = both, y explicit]
coordinates {
(107,   100)(106,   100)(106,   99)(106,    99)(106,    99)(120,    97)(102,    95)(104,    97)(105,    98)
};

mark=o,
mark size=1.4pt,
only marks,
]
plot [error bars/.cd, y dir = both, y explicit]
coordinates {
(106,   98)(106,    98)(107,    99)(106,    98)(104,    86)(103,    97)(104,    98)(105,    99)(105,    97)
};
mark=o,
mark size=1.4pt,
only marks,
]
plot [error bars/.cd, y dir = both, y explicit]
coordinates {
(104,   97)(106,    99)(105,    98)(106,    98)(104,    98)(105,    98)(105,    98)(104,    98)(105,    88)
};
mark=o,
mark size=1.4pt,
only marks,
]
plot [error bars/.cd, y dir = both, y explicit]
coordinates {
(107,   97)(107,    95)(107,    90)(106,    96)(107,    97)(106,    96)(106,    97)(106,    96)(106,    96)(107,    55)(106,    97)(107,    96)
};
%\label{plot_two}
mark=o,
mark size=1.4pt,
only marks,
]
plot [error bars/.cd, y dir = both, y explicit]
coordinates {
(106,97)(106,97)(107,97)(107,95)(107,97)(107,97)(106,97)(107,95)(105,96)(107,97)(106,97)(107,97)
};

mark=o,
mark size=1.4pt,
only marks,
]
plot [error bars/.cd, y dir = both, y explicit]
coordinates {
(107,98)(107,97)(107,97)(106,97)(106,97)(106,97)(106,99)(107,99)(106,97)(104,99)(106,98)(106,49)
};
mark=*,
mark size=1.4pt,
only marks,
]
plot [error bars/.cd, y dir = both, y explicit]
coordinates {
(106,94)(107,97)(106,96)(107,97)(106,97)(106,97)(107,98)(107,96)(106,100)(102,96)(106,97)(106,99)
};

\end{axis}
\end{tikzpicture}
\end{figure}

• You need to specify the error values, as described in the manual. – Torbjørn T. Apr 4 '18 at 9:28
• Thank you but can you please pin point the location where i need to specify the error values. Do i need to calculate mean or standard deviation manually? – Tayyaba Liaqat Apr 4 '18 at 9:30
• It depends on your use case. If you want a fixed value for each plot, then you can specify the value in the \addplot options with y fixed. If you want a separate value for each point (which is what you need to do with y explicit), you do that in the coordinate stream, e.g. (107,100) +- (0,2) for a y-error of 2. If you want to use the standard deviation of each series of data then yes, I do believe you need to calculate it manually. – Torbjørn T. Apr 4 '18 at 9:35
• Your edit doesn't make sense, the standard deviation of a single point is zero. – Torbjørn T. Apr 4 '18 at 9:44

Have a look at section 4.12 Error bars in the pgfplots manual.

When you say y explicit, you need to specify the error value for each point separately in the coordinate stream. For example

(107,100) +- (0,2)


indicates a y-error of 2 and x-error of 0. You must specify both I think, even if you only use the y-error.

But if you want to use the same error value for all the points in a plot, you can use y fixed=<value> instead of y explicit. For example,

\addplot+ [
error bars/.cd,
y dir=both,
y fixed=1
] ...


gives you a y-error of 1 for all the points in the plot. But I don't think there is a built-in way of getting e.g. standard deviation of the plotted data, so if that is what you're after you need to calculate it first, and then use y fixed with the calculated value.

Unrelated comment:

Why do you use this?

\addplot [<some options>] plot[<other options>] ...


You're mixing pgfplots syntax with plain TikZ syntax, the correct would by to have all the options in the same set of brackets, and not use the plot keyword, i.e.

\addplot [<all options>] ...