Error Bars in PGFPlots
Add error bars to visualize uncertainty and data variability.
Basic Y Error Bars
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$}
]
\addplot[
only marks,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(1, 2) +- (0, 0.5)
(2, 4) +- (0, 0.3)
(3, 5) +- (0, 0.8)
(4, 7) +- (0, 0.4)
(5, 8) +- (0, 0.6)
};
\end{axis}
\end{tikzpicture}X and Y Error Bars
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
mark=*,
error bars/.cd,
x dir=both,
x explicit,
y dir=both,
y explicit
] coordinates {
(1, 2) +- (0.2, 0.5)
(2, 4) +- (0.3, 0.3)
(3, 5) +- (0.1, 0.8)
(4, 7) +- (0.4, 0.4)
};
\end{axis}
\end{tikzpicture}Asymmetric Error Bars
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(1, 2) -= (0, 0.3) += (0, 0.8) % Different up/down
(2, 4) -= (0, 0.2) += (0, 0.5)
(3, 5) -= (0, 0.4) += (0, 0.6)
(4, 7) -= (0, 0.3) += (0, 0.9)
};
\end{axis}
\end{tikzpicture}Error Bars from Table
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
error bars/.cd,
y dir=both,
y explicit
] table[
x=x,
y=y,
y error=err
] {
x y err
1 2.1 0.3
2 3.9 0.5
3 6.2 0.4
4 8.0 0.6
5 9.8 0.3
};
\end{axis}
\end{tikzpicture}Error Bars with Line
\begin{tikzpicture}
\begin{axis}[
xlabel={Time (s)},
ylabel={Value}
]
\addplot[
blue,
mark=*,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(0, 1) +- (0, 0.2)
(1, 2.5) +- (0, 0.4)
(2, 3.8) +- (0, 0.3)
(3, 5.2) +- (0, 0.5)
(4, 6.1) +- (0, 0.3)
};
\end{axis}
\end{tikzpicture}Styling Error Bars
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
mark=square*,
blue,
error bars/.cd,
y dir=both,
y explicit,
error bar style={
line width=1pt,
red
},
error mark options={
rotate=90,
mark size=4pt,
red
}
] coordinates {
(1, 2) +- (0, 0.5)
(2, 4) +- (0, 0.3)
(3, 5) +- (0, 0.8)
};
\end{axis}
\end{tikzpicture}Multiple Series with Error Bars
\begin{tikzpicture}
\begin{axis}[
legend pos=north west
]
% Dataset A
\addplot[
blue,
mark=*,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(1, 2) +- (0, 0.3)
(2, 4) +- (0, 0.4)
(3, 5) +- (0, 0.3)
};
% Dataset B
\addplot[
red,
mark=square*,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(1, 1.5) +- (0, 0.2)
(2, 3.5) +- (0, 0.5)
(3, 5.5) +- (0, 0.4)
};
\legend{Dataset A, Dataset B}
\end{axis}
\end{tikzpicture}Error Bars on Bar Charts
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=15pt,
symbolic x coords={A, B, C, D},
xtick=data,
ymin=0
]
\addplot[
fill=blue!60,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(A, 50) +- (0, 5)
(B, 75) +- (0, 8)
(C, 60) +- (0, 6)
(D, 80) +- (0, 10)
};
\end{axis}
\end{tikzpicture}Fixed/Relative Error
\begin{tikzpicture}
\begin{axis}
% Fixed error (same for all points)
\addplot[
blue,
mark=*,
error bars/.cd,
y dir=both,
y fixed=0.5 % Fixed error of 0.5
] coordinates {(1,2) (2,4) (3,5) (4,7)};
% Relative error (percentage of value)
\addplot[
red,
mark=square*,
error bars/.cd,
y dir=both,
y fixed relative=0.1 % 10% relative error
] coordinates {(1,3) (2,5) (3,6) (4,8)};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Box Plots - Statistical summaries
- Scatter Plots - Point data visualization
- Data from Files - Import external data
Error Bars in PGFPlots
Add error bars to visualize uncertainty and data variability.
Basic Y Error Bars
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$}
]
\addplot[
only marks,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(1, 2) +- (0, 0.5)
(2, 4) +- (0, 0.3)
(3, 5) +- (0, 0.8)
(4, 7) +- (0, 0.4)
(5, 8) +- (0, 0.6)
};
\end{axis}
\end{tikzpicture}X and Y Error Bars
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
mark=*,
error bars/.cd,
x dir=both,
x explicit,
y dir=both,
y explicit
] coordinates {
(1, 2) +- (0.2, 0.5)
(2, 4) +- (0.3, 0.3)
(3, 5) +- (0.1, 0.8)
(4, 7) +- (0.4, 0.4)
};
\end{axis}
\end{tikzpicture}Asymmetric Error Bars
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(1, 2) -= (0, 0.3) += (0, 0.8) % Different up/down
(2, 4) -= (0, 0.2) += (0, 0.5)
(3, 5) -= (0, 0.4) += (0, 0.6)
(4, 7) -= (0, 0.3) += (0, 0.9)
};
\end{axis}
\end{tikzpicture}Error Bars from Table
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
error bars/.cd,
y dir=both,
y explicit
] table[
x=x,
y=y,
y error=err
] {
x y err
1 2.1 0.3
2 3.9 0.5
3 6.2 0.4
4 8.0 0.6
5 9.8 0.3
};
\end{axis}
\end{tikzpicture}Error Bars with Line
\begin{tikzpicture}
\begin{axis}[
xlabel={Time (s)},
ylabel={Value}
]
\addplot[
blue,
mark=*,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(0, 1) +- (0, 0.2)
(1, 2.5) +- (0, 0.4)
(2, 3.8) +- (0, 0.3)
(3, 5.2) +- (0, 0.5)
(4, 6.1) +- (0, 0.3)
};
\end{axis}
\end{tikzpicture}Styling Error Bars
\begin{tikzpicture}
\begin{axis}
\addplot[
only marks,
mark=square*,
blue,
error bars/.cd,
y dir=both,
y explicit,
error bar style={
line width=1pt,
red
},
error mark options={
rotate=90,
mark size=4pt,
red
}
] coordinates {
(1, 2) +- (0, 0.5)
(2, 4) +- (0, 0.3)
(3, 5) +- (0, 0.8)
};
\end{axis}
\end{tikzpicture}Multiple Series with Error Bars
\begin{tikzpicture}
\begin{axis}[
legend pos=north west
]
% Dataset A
\addplot[
blue,
mark=*,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(1, 2) +- (0, 0.3)
(2, 4) +- (0, 0.4)
(3, 5) +- (0, 0.3)
};
% Dataset B
\addplot[
red,
mark=square*,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(1, 1.5) +- (0, 0.2)
(2, 3.5) +- (0, 0.5)
(3, 5.5) +- (0, 0.4)
};
\legend{Dataset A, Dataset B}
\end{axis}
\end{tikzpicture}Error Bars on Bar Charts
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=15pt,
symbolic x coords={A, B, C, D},
xtick=data,
ymin=0
]
\addplot[
fill=blue!60,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(A, 50) +- (0, 5)
(B, 75) +- (0, 8)
(C, 60) +- (0, 6)
(D, 80) +- (0, 10)
};
\end{axis}
\end{tikzpicture}Fixed/Relative Error
\begin{tikzpicture}
\begin{axis}
% Fixed error (same for all points)
\addplot[
blue,
mark=*,
error bars/.cd,
y dir=both,
y fixed=0.5 % Fixed error of 0.5
] coordinates {(1,2) (2,4) (3,5) (4,7)};
% Relative error (percentage of value)
\addplot[
red,
mark=square*,
error bars/.cd,
y dir=both,
y fixed relative=0.1 % 10% relative error
] coordinates {(1,3) (2,5) (3,6) (4,8)};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Box Plots - Statistical summaries
- Scatter Plots - Point data visualization
- Data from Files - Import external data