¿Trazar la curva de campana de Gauss basándose en un conjunto de datos?

¿Trazar la curva de campana de Gauss basándose en un conjunto de datos?

Quiero trazar la curva de campana para mostrar la distribución de los datos alrededor de la media, con una y dos desviaciones estándar. Posiblemente, comparando dos conjuntos de datos.

Tengo el siguiente código de @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}

¡El gráfico no se adapta a mis datos!

mis datos son:

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

Respuesta1

Creo que ahora he entendido tu "problema".

En el código de tu pregunta se da xrelativoa μ y σ. Y el rango y no se especifica en absoluto, por lo que ymaxse elige a partir del valor calculado. Pero heightestá dado y, por tanto, independientemente de los valores elegidos de μ y σ, la curva tiene el mismo aspecto. Verá inmediatamente que los valores calculados efectivamente cambian, si simplemente establece un ymaxvalor fijo y luego cambia los valores de μ y σ.

Para demostrarlo, tracé ambas curvas en un axisentorno con solo cambios menores en el código para tener en cuenta los valores cambiantes de μ y σ.

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

imagen que muestra el resultado del código anterior

Respuesta2

¡De alguna manera este código funciona!

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

producción: ingrese la descripción de la imagen aquí

Actualización 1:

Este código se adapta al conjunto de datos. También tracé tres parcelas en el mismo gráfico para mostrar las diferencias. Sin embargo, sigue siendo un problema mostrar correctamente el legendario. El\muLos valores se muestran como trama, por lo tanto, ¡el legendario lo toma como una trama!

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

ingrese la descripción de la imagen aquí

información relacionada