我是 Tex 的新手,想从一组值中绘制高斯分布。我有以下一组值,想为它们绘制正态分布。
2.9954E-09 3.1314E-09 3.1155E-09 3.0940E-09 2.8861E-09 3.0875E-09 2.9685E-09 3.0532E-09 2.9003E-09 3.0931E-09
我尝试使用示例代码,但不知道在哪里输入我的值。
\begin{tikzpicture}
\begin{axis}[
no markers,
domain=0:6,
samples=10,
ymin=0,
axis lines*=left,
xlabel=$x$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
every axis x label/.style={at=(current axis.right of origin),anchor=west},
height=5cm,
width=11cm,
xtick=\empty,
ytick=\empty,
enlargelimits=false,
clip=false,
axis on top,
grid = major,
hide y axis
]
\addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};
\pgfmathsetmacro\valueA{gauss(1,3,1)}
\pgfmathsetmacro\valueB{gauss(2,3,1)}
\draw [gray] (axis cs:1,0) -- (axis cs:1,\valueA)
(axis cs:5,0) -- (axis cs:5,\valueA);
\draw [gray] (axis cs:2,0) -- (axis cs:2,\valueB)
(axis cs:4,0) -- (axis cs:4,\valueB);
\draw [yshift=1.4cm, latex-latex](axis cs:2, 0) -- node [fill=white] {$0.683$} (axis cs:4, 0);
\draw [yshift=0.3cm, latex-latex](axis cs:1, 0) -- node [fill=white] {$0.954$} (axis cs:5, 0);
\node[below] at (axis cs:1, 0) {$\mu - 2\sigma$};
\node[below] at (axis cs:2, 0) {$\mu - \sigma$};
\node[below] at (axis cs:3, 0) {$\mu$};
\end{axis}
\end{tikzpicture}
非常感谢您的帮助。谢谢!
答案1
您的意思是这样的......
(请注意,我已将所有给定的值乘以 10^9,但这不会改变任何内容,因为您没有显示任何值。)
% 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}