Pgfplots 中的误差线增加了吗?

Pgfplots 中的误差线增加了吗?

我从表中读取数据以在 Pgfplots 中创建图表:

x    y   sigma
1    2    0.3
2    4.1  0.4

其中 sigma 是与 y 相关的标准误差。

由于 y 和 sigma 一起是标准正态估计量,因此范围 [y-sigma,y+sigma] 可以解释为 68.3% 的置信区间。(示例中为 [1.7,2.3]。)如果您在 PGFplots 中的数据点中添加错误,则显示标准“错误栏”的范围就是这个范围。现在,我想显示范围 [y-1.96*sigma,y+1.96*sigma],对应于 95% 的置信区间。一种简单的方法是向表中添加一行:

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

然后使用 r95 作为误差变量。但就我而言,这需要大量工作。有没有办法在 PGFPlots 中完成相同的操作,而无需更改表格?我当前的 PGF 图如下所示:

\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}

答案1

您可以使用数学表达式来表示误差线,y error expr=\thisrow{sigma}*1.96方法是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}

相关内容