如何在 pgfplots 中绘制在 x 和 y 坐标上都带有指定误差线的图形?我一直在尝试使用以下方法:
\addplot[scatter,only marks,
scatter src=explicit symbolic,
error bars/.cd,
y dir=both,y explicit,
x dir=both,x explicit]
但是我该如何指定数据中的错误呢?
答案1
为了回答您的问题,此解决方案提供了 3 种不同类型的常见输入。它们是坐标输入、表格输入、外部文件输入。
代码
\documentclass[border=10pt,varwidth]{standalone}%{article}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=newest}
\begin{filecontents}{dataerror.dat}
x y ex ey m
0 0 0.1 0.2 1
1 1 0.1 0.2 2
2 2 0.1 0.2 3
\end{filecontents}
\begin{document}
From coordinate input
\begin{tikzpicture}
\begin{axis}
\addplot+[red,scatter,only marks,
scatter src=explicit symbolic,
error bars/.cd,
y dir=both,y explicit,
x dir=both,x explicit] coordinates{
(0,0) +- (0.1,0.1)[1] % [meta information, viewed as strings]
(1,1) +- (0.1,0.1)[2]
(2,2) +- (0.1,0.1)[3]
};
\end{axis}
\end{tikzpicture}
From table Input
\begin{tikzpicture}
\begin{axis}
\addplot +[green,scatter,only marks,
scatter src=explicit symbolic,
error bars/.cd,
y dir=both,y explicit,x dir=both,x explicit]
table[row sep=crcr, x index=0, y index=1, x error index=2, y error index=3, meta index=4 ]
{0 0 0.1 0.2 1\\
1 1 0.1 0.2 2\\
2 2 0.1 0.2 3\\};
\end{axis}
\end{tikzpicture}
From external table Input
\begin{tikzpicture}
\begin{axis}
\addplot +[brown,scatter,only marks,
scatter src=explicit symbolic,
error bars/.cd,
y dir=both,y explicit,x dir=both,x explicit]
table[x =x, y =y, x error=ex, y error =ey, meta =m]
{dataerror.dat};
\end{axis}
\end{tikzpicture}
\end{document}