Eine Gaußsche Glockenkurve auf Grundlage eines Datensatzes zeichnen?

Eine Gaußsche Glockenkurve auf Grundlage eines Datensatzes zeichnen?

Ich möchte die Glockenkurve zeichnen, um die Verteilung der Daten um den Mittelwert mit einer und zwei Standardabweichungen anzuzeigen. Möglicherweise vergleiche ich zwei Datensätze.

Ich habe folgenden Code von @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}

Die Grafik passt nicht zu meinen Daten!

meine Daten sind:

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

Antwort1

Ich denke, ich habe jetzt Ihr „Problem“ verstanden.

Im Code Ihrer Frage ist x angegebenrelativzu μ und σ. Und der y-Bereich ist überhaupt nicht spezifiziert, ymaxwird also aus dem berechneten Wert gewählt. Aber heighter ist gegeben und daher sieht die Kurve unabhängig von den gewählten Werten von μ und σ gleich aus. Sie würden sofort sehen, dass sich die berechneten Werte tatsächlich ändern, wenn Sie einfach einen festen ymaxWert festlegen und dann die Werte von μ und σ ändern.

Um das zu beweisen, habe ich beide Kurven in einer axisUmgebung mit nur geringfügigen Codeänderungen aufgezeichnet, um die sich ändernden Werte von μ und σ zu berücksichtigen.

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

Bild, das das Ergebnis des obigen Codes zeigt

Antwort2

Irgendwie funktioniert dieser Code!

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

Ausgabe: Bildbeschreibung hier eingeben

Aktualisierung 1:

Dieser Code passt sich dem Datensatz an. Ich habe auch drei Diagramme im selben Diagramm dargestellt, um die Unterschiede zu zeigen. Es bleibt jedoch ein Problem, legendär korrekt anzuzeigen. Die\muWerte werden als Plot angezeigt, daher nimmt das Legendäre es als Plot!

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

Bildbeschreibung hier eingeben

verwandte Informationen