我想绘制钟形曲线来显示数据围绕平均值的分布,具有一到两个标准差。可能的话,比较两个数据集。
我有来自@Stefan Pinnow 的以下代码
% here are your data, just multiplied by 10^9
\begin{filecontents}{data.txt}
2.9954
3.1314
3.1155
3.094
2.8861
3.0875
2.9685
3.0532
2.9003
3.0931
\end{filecontents}
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% use at least this `compat' level so there is no need to prefix
% coordinates with "axis cs:"
compat=1.11,
%
/pgf/declare function={
% `mu' and `sigma' where calculated in Excel using above data
mu=3.03250;
sigma=0.0894182;
% declare gaussian function
gauss(\x)=1/(sigma*sqrt(2*pi))*exp(-((\x-mu)^2)/(2*sigma^2));
% precalculate some values
yA=gauss(mu-2*sigma);
yB=gauss(mu-sigma);
% constant to simply change calculating `domain' and x axis limits
C=2.5;
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% set axis limits and `domain'
xmin=mu-C*sigma,
xmax=mu+C*sigma,
ymin=0,
domain=mu-C*sigma:mu+C*sigma,
% -----------------------------------------------------------------
% nothing changed here
samples=100,
axis lines*=left,
xlabel=$x$,
every axis x label/.style={
at=(current axis.right of origin),
anchor=west,
},
height=5cm,
width=11cm,
xtick=\empty,
ytick=\empty,
axis on top,
hide y axis,
% -----------------------------------------------------------------
% use ticks just at the coordinates of the first `\addplot' ...
xtick=data,
% and show the below labels for these ticks
xticklabels={
$\mu - 2\sigma$,
$\mu - \sigma$,
$\mu$
},
]
% just a dummy plot used for the `xticklabels'
\addplot [draw=none,fill=none] coordinates {
(mu-2*sigma,0)
(mu-sigma,0)
(mu,0)
};
% plot the data point and the corresponding gauss curve
\addplot [only marks,cyan]
table [x index=0,y expr=0] {data.txt};
\addplot [very thick,cyan!50!black] {gauss(x)};
% add some lines and labels
% draw vertical lines
\draw [gray]
(mu-2*sigma,0) -- coordinate (A left) (mu-2*sigma,yA)
(mu+2*sigma,0) -- coordinate (A right) (mu+2*sigma,yA);
\draw [gray]
(mu-sigma,0) -- coordinate (B left) (mu-sigma,yB)
(mu+sigma,0) -- coordinate (B right) (mu+sigma,yB);
% draw labels
\draw [latex-latex]
(A left) -- node [fill=white] {$0.954$} (A right);
\draw [latex-latex]
(B left) -- node [fill=white] {$0.683$} (B right);
\end{axis}
\end{tikzpicture}
\end{document}
该图表不符合我的数据!
我的数据是:
\begin{filecontents}{data.txt}
2.132687
2.634472
2.697368
2.917756
2.582803
2.32906
2.009636
2.483408
1.778771
2.46634
\end{filecontents}
mu=2.403;
sigma=0.327;
答案1
我想现在我已经了解了你的“问题”。
在你的问题代码中给出了 x相对的到 μ 和 σ。y 范围根本没有指定,因此ymax
是从计算值中选择的。但给定了,因此,无论选择 μ 和 σ 的值如何,曲线看起来都一样。如果您只是设置一个固定值,然后更改 μ 和 σ 的值,height
您会立即看到计算值确实发生了变化。ymax
为了证明我在一个环境中绘制了两条曲线axis
,只对代码做了微小的改动以解释 μ 和 σ 值的变化。
% used PGFPlots v1.17
% here are your data, just multiplied by 10^9
\begin{filecontents}{data1.txt}
2.9954
3.1314
3.1155
3.094
2.8861
3.0875
2.9685
3.0532
2.9003
3.0931
\end{filecontents}
\begin{filecontents}{data2.txt}
2.132687
2.634472
2.697368
2.917756
2.582803
2.32906
2.009636
2.483408
1.778771
2.46634
\end{filecontents}
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% use at least this `compat' level so there is no need to prefix
% coordinates with "axis cs:"
compat=1.11,
%
/pgf/declare function={
% `mu' and `sigma' where calculated in Excel using above data
mu1=3.03250;
sigma1=0.0894182;
mu2=2.403;
sigma2=0.327;
% declare gaussian function
gauss(\x,\mu,\sigma)=1/(\sigma*sqrt(2*pi))*exp(-((\x-\mu)^2)/(2*\sigma^2));
% precalculate some values
yA1=gauss(mu1-2*sigma1,mu1,sigma1);
yB1=gauss(mu1-sigma1,mu1,sigma1);
% constant to simply change calculating `domain' and x axis limits
C=2.5;
%
xmin=min(mu1-C*sigma1,mu2-C*sigma2);
xmax=max(mu1+C*sigma1,mu2+C*sigma2);
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% set axis limits and `domain'
xmin=xmin,
xmax=xmax,
ymin=0,
% -----------------------------------------------------------------
% nothing changed here
samples=100,
axis lines*=left,
xlabel=$x$,
every axis x label/.style={
at=(current axis.right of origin),
anchor=west,
},
height=5cm,
width=11cm,
xtick=\empty,
ytick=\empty,
axis on top,
hide y axis,
% -----------------------------------------------------------------
% use ticks just at the coordinates of the first `\addplot' ...
xtick=data,
% and show the below labels for these ticks
xticklabels={
$\mu - 2\sigma$,
$\mu - \sigma$,
$\mu$
},
smooth,
]
% just a dummy plot used for the `xticklabels'
\addplot [draw=none,fill=none] coordinates {
(mu1-2*sigma1,0)
(mu1-sigma1,0)
(mu1,0)
};
% plot the data point and the corresponding gauss curve
\addplot [only marks,cyan]
table [x index=0,y expr=0] {data1.txt};
\addplot [very thick,cyan!50!black,domain=mu1-C*sigma1:mu1+C*sigma1]
{gauss(x,mu1,sigma1)};
% plot the data point and the corresponding gauss curve
\addplot [only marks,orange]
table [x index=0,y expr=0] {data2.txt};
\addplot [very thick,orange!75!black,domain=mu2-C*sigma2:mu2+C*sigma2]
{gauss(x,mu2,sigma2)};
% add some lines and labels
% draw vertical lines
\draw [gray]
(mu1-2*sigma1,0) -- coordinate (A left) (mu1-2*sigma1,yA1)
(mu1+2*sigma1,0) -- coordinate (A right) (mu1+2*sigma1,yA1);
\draw [gray]
(mu1-sigma1,0) -- coordinate (B left) (mu1-sigma1,yB1)
(mu1+sigma1,0) -- coordinate (B right) (mu1+sigma1,yB1);
% draw labels
\draw [latex-latex]
(A left) -- node [fill=white] {$0.954$} (A right);
\draw [latex-latex]
(B left) -- node [fill=white] {$0.683$} (B right);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
不知何故,这个代码有效!
% here are your data, just multiplied by 10^9
\begin{filecontents}{data1.txt}
2.132687
2.634472
2.697368
2.917756
2.582803
2.32906
2.009636
2.483408
1.778771
2.46634
\end{filecontents}
\begin{filecontents}{data.txt}
2.065643
2.031713
2.055865
2.365157
2.227517
2.008509
2.790536
2.167367
2.269939
2.065643
\end{filecontents}
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% use at least this `compat' level so there is no need to prefix
% coordinates with "axis cs:"
compat=1.11,
%
/pgf/declare function={
% `mu' and `sigma' where calculated in Excel using above data
mu=2.205;
sigma=0.234;
% declare gaussian function
gauss(\x)=1/(sigma*sqrt(2*pi))*exp(-((\x-mu)^2)/(2*sigma^2));
% precalculate some values
yA=gauss(mu-2*sigma);
yB=gauss(mu-sigma);
% constant to simply change calculating `domain' and x axis limits
C=4
;
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% set axis limits and `domain'
xmin=mu-C*sigma,
xmax=mu+C*sigma,
ymin=0,
domain=mu-C*sigma:mu+C*sigma,
% -----------------------------------------------------------------
% nothing changed here
samples=100,
axis lines*=left,
xlabel=$x$,
every axis x label/.style={
at=(current axis.right of origin),
anchor=west,
},
height=5cm,
width=11cm,
xtick=\empty,
ytick=\empty,
axis on top,
hide y axis,
% -----------------------------------------------------------------
% use ticks just at the coordinates of the first `\addplot' ...
xtick=data,
% and show the below labels for these ticks
xticklabels={
$\mu - 2\sigma$,
$\mu - \sigma$,
$\mu$,
$\mu + \sigma$,
$\mu + 2\sigma$
},
]
% just a dummy plot used for the `xticklabels'
\addplot [draw=none,fill=none] coordinates {
(mu-2*sigma,0)
(mu-sigma,0)
(mu,0)
(mu+sigma,0)
(mu+2*sigma,0)
};
% plot the data point and the corresponding gauss curve
\addplot [only marks,blue]
table [x index=0,y expr=0] {data.txt};
\addplot [very thick,red!50!black] {gauss(x)};
% add some lines and labels
% draw vertical lines
\draw [gray]
(mu-2*sigma,0) -- coordinate (A left) (mu-2*sigma,yA)
(mu+2*sigma,0) -- coordinate (A right) (mu+2*sigma,yA);
\draw [gray]
(mu-sigma,0) -- coordinate (B left) (mu-sigma,yB)
(mu+sigma,0) -- coordinate (B right) (mu+sigma,yB);
% draw labels
\draw [latex-latex]
(A left) -- node [fill=white] {$95 \%$} (A right);
\draw [latex-latex]
(B left) -- node [fill=white] {$68 \%$} (B right);
\end{axis}
\end{tikzpicture}
\end{document}
更新 1:
此代码可适应数据集。我还在同一张图中绘制了三张图来显示差异。但是,正确显示传奇仍然存在问题。\亩价值观体现为剧情,所以传奇就当是剧情吧!
% used PGFPlots v1.17
% here are your data, just multiplied by 10^9
% TEE
\begin{filecontents}{data1.txt}
2.132687
2.634472
2.697368
2.917756
2.582803
2.32906
2.009636
2.483408
1.778771
2.46634
\end{filecontents}
% ICE
\begin{filecontents}{data2.txt}
2.065643
2.031713
2.055865
2.365157
2.227517
2.008509
2.790536
2.167367
2.269939
2.065643
\end{filecontents}
% L742
\begin{filecontents}{data3.txt}
1.67097
1.65911
2.96315
2.46577
1.61159
1.46357
1.59512
1.87797
2.37143
1.16881
\end{filecontents}
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% use at least this `compat' level so there is no need to prefix
% coordinates with "axis cs:"
compat=1.11,
%
/pgf/declare function={
% `mu' and `sigma' where calculated in Excel using above data
mu1=2.40;
sigma1=0.33;
mu2=2.2;
sigma2=0.22;
mu3=1.88;
sigma3=0.52;
% declare gaussian function
gauss(\x,\mu,\sigma)=1/(\sigma*sqrt(2*pi))*exp(-((\x-\mu)^2)/(2*\sigma^2));
% precalculate some values
yA1=gauss(mu1-2*sigma1,mu1,sigma1);
yB1=gauss(mu1-sigma1,mu1,sigma1);
yA2=gauss(mu2-2*sigma2,mu2,sigma2);
yB2=gauss(mu2-sigma2,mu2,sigma2);
yA3=gauss(mu3-2*sigma3,mu3,sigma3);
yB3=gauss(mu3-sigma3,mu3,sigma3);
% constant to simply change calculating `domain' and x axis limits
C=2.5;
%
xmin=min(mu1-C*sigma1,mu2-C*sigma2,mu3-C*sigma3);
xmax=max(mu1+C*sigma1,mu2+C*sigma2,mu3+C*sigma3);
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend pos=north west,
% set axis limits and `domain'
xmin=xmin,
xmax=xmax,
ymin=0,
% -----------------------------------------------------------------
% nothing changed here
samples=100,
axis lines*=left,
xlabel=\tiny{$Error$},
every axis x label/.style={
at=(current axis.right of origin),
anchor=west,
},
height=5cm,
width=11cm,
xtick=\empty,
ytick=\empty,
axis on top,
hide y axis,
% -----------------------------------------------------------------
% use ticks just at the coordinates of the first `\addplot' ...
xtick=data,
% and show the below labels for these ticks
xticklabels={
$\mu_{1}$,
$\mu_{2}$,
$\mu_{3}$
},
smooth,
]
% just a dummy plot used for the `xticklabels'
\addplot [draw=none] coordinates {
(mu1,0)
(mu2,0)
(mu3,0)
};
\addlegendentry[draw = none]{\tiny{$\mu_{1}=2.40$, $\mu_{2}=2.2$, $\mu_{3}=1.88$}}
% plot the data point and the corresponding gauss curve TEE
\addplot [very thick,blue,domain=mu1-C*sigma1:mu1+C*sigma1]
{gauss(x,mu1,sigma1)};
\addlegendentry{\footnotesize{TEE}}
% plot the data point and the corresponding gauss curve ICE
\addplot [very thick,red,domain=mu2-C*sigma2:mu2+C*sigma2]
{gauss(x,mu2,sigma2)};
\addlegendentry{\footnotesize{AcuNav (ICE)}}
% plot the data point and the corresponding gauss curve 742
\addplot [very thick,green,domain=mu3-C*sigma3:mu3+C*sigma3]
{gauss(x,mu3,sigma3)};
\addlegendentry{\footnotesize{L742}}
% add some lines and labels
% draw vertical lines
%TEE
\draw [blue,very thick,fill=blue]
(mu1,0) -- coordinate (A left) (mu1,yA1);
%ICE
\draw [red,very thick,fill=red]
(mu2,0) -- coordinate (A left) (mu2,yA2);
%L742
\draw [fill=green,green,very thick]
(mu3,0) -- coordinate (A left) (mu3,yA3);
% Plot the dots
% TEE
\addplot [only marks,blue]
table [x index=0,y expr=0] {data1.txt};
% ICE
\addplot [only marks,red]
table [x index=0,y expr=0] {data2.txt};
% 742
\addplot [only marks,green]
table [x index=0,y expr=0] {data3.txt};
\end{axis}
\end{tikzpicture}
\end{document}