我想繪製鐘形曲線來顯示資料圍繞平均值的分佈,具有 1 個和 2 個標準差。可能,比較兩個資料集。
我有以下來自 @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}